Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
107
rated 0 times [  109] [ 2]  / answers: 1 / hits: 41881  / 11 Years ago, thu, may 30, 2013, 12:00:00

I have a javascript function written to validate a field on my form. This function is supposed to make sure the field is not empty, does not exceed the limit of 35 characters and only contains alphabetic characters and a hyphen(-). I had code to make sure the field is not empty and that it does not exceed 35 characters which worked fine but i added code to validate the field to the usable characters and i tested it out by leaving the field empty to make sure that still worked but when i hit the submit button the function didn't seem to validate at all, didn't give me an alert and just submitted. Here is my code:



function validateFamily()
{
var family=document.getElementById('family');
var stringf = document.getElementById('family').value;
if (family.value==)
{
alert(Family name must be filled out);
return false;
}
else if (document.getElementById('family').value.length > 35)
{
alert(Family name cannot be more than 35 characters);
return false;
}
else if (/[^a-zA-Z-]/.test( stringf ))
{
alert(Family name can only contain alphanumeric characters and hypehns(-))
return false;
}
return true;
}

<form name=eoiform method=POST action=<?php echo $_SERVER[PHP_SELF];?> id=eoi onsubmit=return validateFamily() && validateGiven() && validateMaleFemale() && validDate() && validateAddress() && validatePost() && validateParent() && validateWork() && validateHome() && validateMob() && validateCheckBoxes();>

<b>Student's Family Name</b>
<br>
<input type=text id=family name=family /><?php echo $strmsgf; ?>

<input type=submit name=submit id=submit value=submit />

</form>


Could anyone show me how to fix my code?


More From » forms

 Answers
-2

UPDATED:



Sorry, the problem is with your regex, i missed that, change to this its fully working:



  var ck_password =  /^[A-Za-z0-9-]/;

if(!ck_password.test(stringf))
{

alert(Family name can only contain alphanumeric characters and hypehns(-))

}


Console in chrome, go to the OPTIONS in the right top coner, select TOOLS, then DEVELOPER TOOLS.


[#77911] Wednesday, May 29, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
devinjadong

Total Points: 711
Total Questions: 117
Total Answers: 100

Location: Andorra
Member since Sat, May 27, 2023
1 Year ago
devinjadong questions
Thu, Feb 17, 22, 00:00, 2 Years ago
Wed, Dec 8, 21, 00:00, 2 Years ago
Tue, Oct 27, 20, 00:00, 4 Years ago
Fri, Oct 18, 19, 00:00, 5 Years ago
;