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="JavaScript" type="text/JavaScript">
<!--
//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 == "@"
{
// 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 == "#"
{
// 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 = "The value in the " +
current_form[counter].name +
" field is invalid.\n"
var mask_string = current_form[counter].mask_example
mask_error += "\nThe value must be entered using the following format:\n\n " +
mask_string
// Display the message & return false to bypass the calculation
alert(mask_error)
}
}
}
}
function its_a_letter(character) {
var lowercase_letters = "abcdefghijklmnopqrstuvwxyz"
var uppercase_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// 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 = "0123456789"
// 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("@"
;
dotpos=value.lastIndexOf("."
;
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==""
{if (alertbox!=""
{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)=="i"
{checkvalue=parseInt(value); if (value.indexOf("."
!=-1) {checkvalue=checkvalue+1}};
}
if ((parseFloat(min)==min && value.length<min) || (parseFloat(max)==max
&& value.length>max) || value!=checkvalue)
{if (alertbox!=""
{alert(alertbox);} return false;}
else {return true;}
}
}
function formvalidationlogin(thisform)
{
with (thisform)
{
if (emptyvalidation(UserName,"Please complete all required fields"
==false) {UserName.focus(); return false;};
if (emptyvalidation(NINumber,"Please complete all required fields"
==false) {NINumber.focus(); return false;};
if (emptyvalidation(Email,"Please complete all required fields"
==false) {Email.focus(); return false;};
if (emptyvalidation(EmailReEntered,"Please complete all required fields"
==false) {EmailReEntered.focus(); return false;};
}
}
// -->
</script>
<!-- ********* END OF FORM VALIDXN ************* -->
<body>
<form action="submit.asp" method="post" name="feedbackform" id="feedbackform" onsubmit="return formvalidationlogin(this); validate(this)">
<table>
<tr><td>NI<input type="text" name="NINumber"><br />
Email<input type="text" name="Email"><br />
Email Reenter<input type="text" name="EmailReEntered"><br />
UN<input type="text" name="UserName"><br />
<input type="image" id="button" src="images/big_01.gif" alt="submit" WIDTH="65" HEIGHT="27" />
</td>
</tr>
</form>
<script language="JavaScript" type="text/javascript">
<!--
document.feedbackform.NINumber.mask = "@@######@"
document.feedbackform.NINumber.mask_example = "XX999999X"
document.feedbackform.Email.mask = "@@@@@@"
document.feedbackform.Email.mask_example = "marcus"//-->
</script>
</body>
</html>
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="JavaScript" type="text/JavaScript">
<!--
//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 == "@"
// 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 == "#"
// 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 = "The value in the " +
current_form[counter].name +
" field is invalid.\n"
var mask_string = current_form[counter].mask_example
mask_error += "\nThe value must be entered using the following format:\n\n " +
mask_string
// Display the message & return false to bypass the calculation
alert(mask_error)
}
}
}
}
function its_a_letter(character) {
var lowercase_letters = "abcdefghijklmnopqrstuvwxyz"
var uppercase_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// 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 = "0123456789"
// 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("@"
dotpos=value.lastIndexOf("."
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==""
{if (alertbox!=""
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)=="i"
{checkvalue=parseInt(value); if (value.indexOf("."
}
if ((parseFloat(min)==min && value.length<min) || (parseFloat(max)==max
&& value.length>max) || value!=checkvalue)
{if (alertbox!=""
else {return true;}
}
}
function formvalidationlogin(thisform)
{
with (thisform)
{
if (emptyvalidation(UserName,"Please complete all required fields"
if (emptyvalidation(NINumber,"Please complete all required fields"
if (emptyvalidation(Email,"Please complete all required fields"
if (emptyvalidation(EmailReEntered,"Please complete all required fields"
}
}
// -->
</script>
<!-- ********* END OF FORM VALIDXN ************* -->
<body>
<form action="submit.asp" method="post" name="feedbackform" id="feedbackform" onsubmit="return formvalidationlogin(this); validate(this)">
<table>
<tr><td>NI<input type="text" name="NINumber"><br />
Email<input type="text" name="Email"><br />
Email Reenter<input type="text" name="EmailReEntered"><br />
UN<input type="text" name="UserName"><br />
<input type="image" id="button" src="images/big_01.gif" alt="submit" WIDTH="65" HEIGHT="27" />
</td>
</tr>
</form>
<script language="JavaScript" type="text/javascript">
<!--
document.feedbackform.NINumber.mask = "@@######@"
document.feedbackform.NINumber.mask_example = "XX999999X"
document.feedbackform.Email.mask = "@@@@@@"
document.feedbackform.Email.mask_example = "marcus"//-->
</script>
</body>
</html>