Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form validation - mask, 2 values the same and alpha characters - help!

Status
Not open for further replies.

leeolive

Programmer
May 14, 2002
46
0
0
GB
Hi

I have some js validation code which I need to do 3 checks.

- Check that a value (NINumber) entered is entered in this format: XX999999X
- Check that an email (Email & ReEnteredEmail) entered is the same as a reentered email
- check that a username (UserName) consists of 6(min and max) alpha letters

The validation code I am working with is below
Where am I going wrong? When I enter a value it just submits. I need to call 2 functions but neither are working. *Really* appreciate some help!! Thanks Lee


<html>
<head><title></title>
</head>
<body>

<!-- ********* FORM VALIDXN ************* -->
<script language=&quot;JavaScript&quot; type=&quot;text/JavaScript&quot;>
<!--

//checks mask of ninumber

function check_mask(field_value, mask, case_sensitive) {

// If the strings are different lengths, return false
if (field_value.length != mask.length) {
return false
}

// Run through each character in the mask
for (var counter = 0; counter < mask.length; counter++) {

// Get the current mask and field_value characters
current_mask = mask.charAt(counter)
current_char = field_value.charAt(counter)

// Is the mask character a letter?
if (current_mask == &quot;@&quot;) {

// If so, then if the current character isn't a letter, return false
if (!its_a_letter(current_char)) {
return false
}

}
// Is the mask character a digit?
else if (current_mask == &quot;#&quot;) {

// If so, then if the current character isn't a digit, return false
if (!its_a_digit(current_char)) {
return false
}
}
// Otherwise, are the characters the same?
else {

// Is this a case-sensitive mask?
if (!case_sensitive) {

// If not, then use only uppercase characters
current_mask = current_mask.toUpperCase()
current_char = current_char.toUpperCase()
}
if (current_mask != current_char) {
return false
}
}
}

// If we get this far, we have a match
return true
}

function validate(current_form) {

// Loop through the form fields
for (var counter = 0; counter < current_form.length; counter++) {

// Is a mask defined for this field?
if (current_form[counter].mask) {

// If so, send the field to the check_mask() function
if (!check_mask(current_form[counter].value, current_form[counter].mask, false)) {

// If the value isn't within the range, set up an error message
var mask_error = &quot;The value in the &quot; +
current_form[counter].name +
&quot; field is invalid.\n&quot;

var mask_string = current_form[counter].mask_example

mask_error += &quot;\nThe value must be entered using the following format:\n\n &quot; +
mask_string

// Display the message & return false to bypass the calculation
alert(mask_error)

}
}
}
}

function its_a_letter(character) {

var lowercase_letters = &quot;abcdefghijklmnopqrstuvwxyz&quot;
var uppercase_letters = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;

// If it's not in the lowercase_letters string or the
// uppercase_letters string, then it's not a letter,
// so return false

if (lowercase_letters.indexOf(character) == -1 &&
uppercase_letters.indexOf(character) == -1) {
return false
}

// Otherwise, it's a letter, so return true
return true
}

function its_a_digit(character) {

var digit_characters = &quot;0123456789&quot;

// If it's not in the digit_characters string,
// then it's not a digit so return false

if (digit_characters.indexOf(character) == -1) {
return false
}

// Otherwise, it's a digit, so return true
return true
}


// end of mask check


function emailvalidation(entered, alertbox)
{
// E-mail-Validation (c) Henrik Petersen / NetKontoret
// Explained at // Please do not remove the this line and the two lines above.
with (entered)
{
apos=value.indexOf(&quot;@&quot;);
dotpos=value.lastIndexOf(&quot;.&quot;);
lastpos=value.length-1;
if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2)
{if (alertbox) {alert(alertbox);} return false;}
else {return true;}
}
}

function emptyvalidation(entered, alertbox)
{
// Emptyfield-Validation (c) Henrik Petersen / NetKontoret
// Explained at // Please do not remove the this line and the two lines above.
with (entered)
{
if (value==null || value==&quot;&quot;)
{if (alertbox!=&quot;&quot;) {alert(alertbox);} return false;}
else {return true;}
}
}

function digitvalidation(entered, min, max, alertbox, datatype)
{
// Digit Validation by Henrik Petersen / NetKontoret
// Explained at
//// Please do not remove this line and the two lines above.
with (entered)
{
checkvalue=parseFloat(value);
if (datatype)
{smalldatatype=datatype.toLowerCase();
if (smalldatatype.charAt(0)==&quot;i&quot;)
{checkvalue=parseInt(value); if (value.indexOf(&quot;.&quot;)!=-1) {checkvalue=checkvalue+1}};
}
if ((parseFloat(min)==min && value.length<min) || (parseFloat(max)==max
&& value.length>max) || value!=checkvalue)
{if (alertbox!=&quot;&quot;) {alert(alertbox);} return false;}
else {return true;}
}
}

function formvalidationlogin(thisform)
{
with (thisform)
{
if (emptyvalidation(UserName,&quot;Please complete all required fields&quot;)==false) {UserName.focus(); return false;};
if (emptyvalidation(NINumber,&quot;Please complete all required fields&quot;)==false) {NINumber.focus(); return false;};
if (emptyvalidation(Email,&quot;Please complete all required fields&quot;)==false) {Email.focus(); return false;};
if (emptyvalidation(EmailReEntered,&quot;Please complete all required fields&quot;)==false) {EmailReEntered.focus(); return false;};

}
}

// -->
</script>
<!-- ********* END OF FORM VALIDXN ************* -->

<body>
<form action=&quot;submit.asp&quot; method=&quot;post&quot; name=&quot;feedbackform&quot; id=&quot;feedbackform&quot; onsubmit=&quot;return formvalidationlogin(this); validate(this)&quot;>
<table>
<tr><td>NI<input type=&quot;text&quot; name=&quot;NINumber&quot;><br />
Email<input type=&quot;text&quot; name=&quot;Email&quot;><br />
Email Reenter<input type=&quot;text&quot; name=&quot;EmailReEntered&quot;><br />
UN<input type=&quot;text&quot; name=&quot;UserName&quot;><br />
<input type=&quot;image&quot; id=&quot;button&quot; src=&quot;images/big_01.gif&quot; alt=&quot;submit&quot; WIDTH=&quot;65&quot; HEIGHT=&quot;27&quot; />
</td>
</tr>
</form>

<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
<!--

document.feedbackform.NINumber.mask = &quot;@@######@&quot;
document.feedbackform.NINumber.mask_example = &quot;XX999999X&quot;
document.feedbackform.Email.mask = &quot;@@@@@@&quot;
document.feedbackform.Email.mask_example = &quot;marcus&quot;//-->
</script>
</body>
</html>
 
the problem is the onsubmit in the form tag.

you have:

onsubmit-&quot;return formvalidationlogin(this); validate(this)&quot;

validate(this) never get called as it &quot;return&quot; after formvalidationlogin(this).

try this:

onsubmit=&quot;return (formvalidationlogin(this) & validate(this))>0&quot;

it should solve your problem.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top