megz90 Posted March 9, 2008 Share Posted March 9, 2008 hi i think ive missed a { or a } somewhere my JS will let the dater pass as valid when it is not. ive got this form with 12 fields in it. 9 of 12 are required 2 of 12 is numbers only 1 of 12 is email this is what ive done so far. check for the required fields not being left blank (havent got to the check for numbers only or letters only yet (pointers would be handy)) when checking the email address it seems to be ok and lets the data through as valid. <script language="javascript"> invalid = "/:,;" function validateForm(form) { if (form.dbOwnerId.value == "" || form.dbFname.value == "" || form.dbLname.value == "" || form.dbemail.value == "" || form.dbmainphone.value == "" || form.dbaddress.value == "" || form.dbcity.value == "" || form.dbpostcode.value == "" || form.dbpassword.value == "") { alert ("Missing compulsory fields") form.dbFname.focus() return false } dbemail = form.dbemail.value howmany = dbemail.length alert ("length is "+ howmany) if (dbemail != "") { for (i=0; i<invalidchars.length; i++) { badchar = invalidchars.charAt(i) if (dbemail.indexOf(badchar, 0) > -1) { alert ("Invalid emaillll") form.dbemail.focus() return false } } } if (dbemail.indexOf("@", 0) == -1) { alert ("Invalid m_email m_add") form.m_email.focus() return false } { return true } } </script> this is the data from my form <form method="post" onsubmit="return validateForm(this)" action="register.php"> the input names are as below name="dbOwnerId" required any name="dbFname" required letters name="dbLname" required letters name="dbemail" required email name="dbmainphone" required num name="dbotherphone" num name="dbaddress" required any name="dbaddress1" any name="dbcity" required letters name="dbpostcode" required mixed max 9 name="dbnotes" any name="dbpassword" required any can anyone see if im missing something? Quote Link to comment https://forums.phpfreaks.com/topic/95147-js-form-validator/ Share on other sites More sharing options...
Psycho Posted March 9, 2008 Share Posted March 9, 2008 Yeah, there are some problems with the opening and closing brackets. Your opening script tag should be using "type" instead of "language". Here are some validations that should work for you below: The alpha check allows letters, the space, the hyphen and the apostrophe (since names can contain those characters). The phone check allows numbers, parenthesis, space, and the hyphen The email check only allows properly formatted email addresses. You can get more restrictive on these validations (e.g. phone numbers must be 7 or 10 characters), but I included validations based upon your post. <script type="text/javascript"> function validateForm(form) { var fe = form.elements; //validate owner id if (!requiredCheck(fe['dbOwnerId'], 'Owner')) return false; //validate first name if (!requiredCheck(fe['dbFname'], 'First Name')) return false; if (!validAlpha(fe['dbFname'], 'First Name')) return false; //validate last name if (!requiredCheck(fe['dbLname'], 'Last Name')) return false; if (!validAlpha(fe['dbLname'], 'Last Name')) return false; //validate email address if (!requiredCheck(fe['dbemail'], 'Email address')) return false; if (!validEmail(fe['dbemail'], 'Email address')) return false; //validate main phone if (!requiredCheck(fe['dbmainphone'], 'Main Phone')) return false; if (!validPhone(fe['dbmainphone'], 'Main Phone')) return false; //validate other phone if (!validPhone(fe['dbotherphone'], 'Other Phone')) return false; //validate address if (!requiredCheck(fe['dbaddress'], 'Address')) return false; //validate address 1 if (!requiredCheck(fe['dbcity'], 'City')) return false; if (!validAlpha(fe['dbcity'], 'City')) return false; //validate post code if (!requiredCheck(fe['dbpostcode'], 'Post Code')) return false; //validate password if (!requiredCheck(fe['dbpassword'], 'Password')) return false; return true; } function requiredCheck(fieldObj, fieldName) { if (!fieldObj.value) { alert(fieldName+' is required.'); fieldObj.focus(); return false; } return true; } function validEmail(fieldObj, fieldName) { validEmailRegEx = /^[\w\+-]+([\.]?[\w\+-])*@[a-zA-Z0-9]{2,}([\.-]?[a-zA-Z0-9]{2,})*\.[a-zA-Z]{2,4}$/ if (!fieldObj.value.match(validEmailRegEx)) { alert(fieldName+' is not in the proper format.'); fieldObj.focus(); return false; } return true; } function validAlpha(fieldObj, fieldName) { validAlphaRegEx = /^[a-zA-Z '-]*$/ if (!fieldObj.value.match(validAlphaRegEx)) { alert(fieldName+' contains invalid characters.'); fieldObj.focus(); return false; } return true; } function validPhone(fieldObj, fieldName) { validNumberRegEx = /^[\d \-()]*$/ if (!fieldObj.value.match(validNumberRegEx)) { alert(fieldName+' contains invalid characters.'); fieldObj.focus(); return false; } return true; } </script> Quote Link to comment https://forums.phpfreaks.com/topic/95147-js-form-validator/#findComment-487443 Share on other sites More sharing options...
megz90 Posted March 9, 2008 Author Share Posted March 9, 2008 Thanks loads for your help, this script that you have made has held up so far and i will be able to use it to show myself how to do other form for my coursework aswell thanks. Quote Link to comment https://forums.phpfreaks.com/topic/95147-js-form-validator/#findComment-488030 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.