|
Post by Prostyler on Apr 30, 2008 8:59:57 GMT
I am trying to find some good javascript form validation, to validate my php form.
I want to validate normal fields so that data within those fields must be entered. I want to validate an email field, so that only a correctly written email address is valid (name@domain.whatever) I want to validate a phone number so that it is a valid uk number. I want to validate a postcode so that it is written correctly (LLNNLL) L=Letter N=Number. And that's all.
I have searched through google, but sites only have a couple of those validations, and different sites use different techniques, so I need someone or somewhere that can show me all those validations using the same technique.
Please help,
Thanks,
Ricky
|
|
|
Post by Dalton on May 7, 2008 1:06:33 GMT
Ok. For normal fields I would do this
<script> function validate_form ( ) valid = true if ( document.contact_form.contact_name.value == "" ) alert ( "Please fill in the (Form Name)." ); valid = false; return valid; </script>
For Email... Hmm.. Lets see... (Sorry, thinking aloud.)
<script> function EmailValidthingihopel() { email = document.f1.Email.value AtPos = email.indexOf("@") StopPos = email.lastIndexOf(".") Message = "" if (email == "") Message = "Not a valid Email address" + "\n" if (AtPos == -1 || StopPos == -1) Message = "Not a valid email address" if (StopPos < AtPos) Message = "Not a valid email address" if (StopPos - AtPos == 1) Message = "Not a valid email address" return Message </script>
Sorry, If statements were the only way I could think to do it right now, I am on the phone and it has me quite distracted.
Ok, I looked at UK Phone numbers and I cant make sure it is a completely valid number. I can make sure it uses the right structure but I cant make it make sure it is a real number.. Sorry.
Ok. Post codes. I think I can do Letter Letter Number Number Letter Letter.
<script> function checkpostalcodemaybe(pc) var test = pc; var size = test.length test = test.toUpperCase(); while (test.slice(0,1) == " " test = test.substr(1,size-1);size = test.length while(test.slice(size-1,size) == " ") test = test.substr(0,size-1);size = test.length if (size == 0) return "please enter a valid postcode"; document.details.pcode.value = test; if (size <> 6{ return test + " is not a valid postcode - wrong length"; if (!(isNaN(test.charAt(0)))) return test + " is not a valid postcode - cannot start with a number"; if (isNaN(test.charAt(size-3))) return test + " is not a valid postcode - alpha character in wrong position"; if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule return test + " is not a valid postcode - number in wrong position"; if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule return test + " is not a valid postcode - number in wrong position"; if (!(test.charAt(size-4) == " ")){//space in position length-3 rule return test + " is not a valid postcode - space in wrong position"; count1 = test.indexOf(" ");count2 = test.lastIndexOf(" "); if (count1 != count2){//only one space rule return "please enter a valid postcode"; return test + " is not a valid postcode - only one space allowed"; return "OK"; </script>
Borrowed the idea from some random person on a blog named sabah. His/Hers was for Indian postcodes I think though.. But this one does letter letter number number letter letter.
Edit: Added the <script>.. Forgot about those little boogers.
|
|
|
Post by Prostyler on May 7, 2008 11:45:35 GMT
Fantastic help Dalton!!! Thanks VERY much! Just 2 things I'd like to ask. The first thing is, some Post Codes in the UK, like London and Manchester, don't use the same structure as all other UK post codes (ie. LLNNLL). Therefore, could the validation for the postcode just be that it must consist of 6 characters, letters and numbers only... The second thing is, how do I then apply these validations to my form? Thanks so much! I hope I'm not asking too much.
|
|
|
Post by Dalton on May 7, 2008 14:18:29 GMT
Fantastic help Dalton!!! Thanks VERY much! Just 2 things I'd like to ask. The first thing is, some Post Codes in the UK, like London and Manchester, don't use the same structure as all other UK post codes (ie. LLNNLL). Therefore, could the validation for the postcode just be that it must consist of 6 characters, letters and numbers only... The second thing is, how do I then apply these validations to my form? Thanks so much! I hope I'm not asking too much. Ill fix the post code script this afternoon. As for applying the validations. In the first line of each script it has something like function validate_form ( ) In the parentheses you put the forms name and it applies it. Im almost certain that is where you put it. I havent done Client-side Web Programming in a while and I am trying to get re-practiced. I know the Javascript is right, I am just forgetting my HTML.
|
|
|
Post by Prostyler on May 7, 2008 15:12:14 GMT
Thanks SO much Dalton! This is my contact form that I have created so far: <form method="POST" name="enquiryForm" action="contact.php"> <table cellpadding="0" cellspacing="0" id="contacttable"> <tr> <td>First Name</td> <td><input type="text" name="FirstName" /></td> </tr> <tr class="trblue"> <td>Last Name</td> <td><input type="text" name="LastName" /></td> </tr> <tr> <td>Email Address</td> <td><input type="text" name="EmailFrom" /></td> </tr> <tr class="trblue"> <td>Contact Number</td> <td><input type="text" name="ContactNumber" /></td> </tr> <tr> <td>Company Name</td> <td><input type="text" name="CompanyName" /></td> </tr> <tr class="trblue"> <td>Type Of Business</td> <td><input type="text" name="TypeOfBusiness" /></td> </tr> <tr> <td>Address Line 1</td> <td><input type="text" name="AddressLine1" /></td> </tr> <tr class="trblue"> <td>Address Line 2</td> <td><input type="text" name="AddressLine2" /></td> </tr> <tr> <td>Address Line 3</td> <td><input type="text" name="AddressLine3" /></td> </tr> <tr class="trblue"> <td>Town / City</td> <td><input type="text" name="TownCity" /></td> </tr> <tr> <td>County</td> <td><input type="text" name="County" /></td> </tr> <tr class="trblue"> <td>Postcode</td> <td><input type="text" name="Postcode" /></td> </tr> <tr> <td>Enquiry Details</td> <td><textarea name="EnquiryDetails"></textarea></td> </tr> <tr class="trblue"> <td>Required Quantity</td> <td><input type="text" name="RequiredQuantity" /></td> </tr> <tr> <td>No. Of Print Colours</td> <td><input type="text" name="NoOfPrintColours" /></td> </tr> <tr class="trblue"> <td>Request A Sample</td> <td><input type="checkbox" name="RequestASample" value="Yes" class="checkbox" /></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" name="submit" value="Send Enquiry" class="submit"/></td> </tr> </table> </form> You may find it easier to explain to me if you can see what I have already. Thanks alot!!!
|
|
|
Post by Dalton on May 7, 2008 15:25:39 GMT
Alright, Ill look at it this afternoon and Ill edit this post.
Alright.
I just looked at this and the scripts that I made were assuming this wasnt all on the same form. Ill ask James if I can apply the form name part of the JS to this tag <input type="text" name="Input name here" /> because I havent messed with this stuff since I picked up Perl, C++, and Python.
|
|
|
Post by Dalton on May 7, 2008 21:20:28 GMT
Double post FTL
Just thought about this.. You arent just using JS to validate your forms are you?
You know.. Kinda like this for a Serverside email Validator
function check_email_address($email) if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) $email_array = explode("@", $email); $local_array = explode(".", $email_array[0]); for ($i = 0; $i < sizeof($local_array); $i++) if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) return false; if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) $domain_array = explode(".", $email_array[1]); if (sizeof($domain_array) < 2) return false; for ($i = 0; $i < sizeof($domain_array); $i++) if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) return false; return true; }
|
|
|
Post by Prostyler on May 8, 2008 6:54:25 GMT
Morning! Ok, umm, I am using just JS to validate the form... Hmmmmmmmmmmmmmmmmmmmm, I have no idea what to do with this JS, so thanks for all the help so far!
|
|
|
Post by Dalton on May 8, 2008 20:12:30 GMT
Morning! Ok, umm, I am using just JS to validate the form... Hmmmmmmmmmmmmmmmmmmmm, I have no idea what to do with this JS, so thanks for all the help so far! Ok. For every input box that isnt Email Phone or postcode take this part of the code and do this function validate_form ( )
and fill in the input box name like this function validate_form (Input box name here )
For the Email Phone and Postcode take their respective code and put the input box name in the first line like you did there.
|
|
|
Post by Prostyler on May 13, 2008 10:04:07 GMT
Hi mate, I'm still really struggling to make this work. Can you possibly try and provide me some more support for this? Thanks so much!
|
|
|
Post by Dalton on May 13, 2008 21:51:07 GMT
Hi mate, I'm still really struggling to make this work. Can you possibly try and provide me some more support for this? Thanks so much! Yes. But Can we wait till friday afternoon? Ive got AP exams all week. T_T
|
|
|
Post by Daniel on May 14, 2008 5:38:33 GMT
Just thought about this.. You arent just using JS to validate your forms are you? You know.. Kinda like this for a Serverside email Validator function check_email_address($email) if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) $email_array = explode("@", $email); $local_array = explode(".", $email_array[0]); for ($i = 0; $i < sizeof($local_array); $i++) if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) return false; if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) $domain_array = explode(".", $email_array[1]); if (sizeof($domain_array) < 2) return false; for ($i = 0; $i < sizeof($domain_array); $i++) if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) return false; return true; }This is definitely the way to go. Validate input fields with PHP.It's a much more secure and effective validation method. Someone could easily just temporarily disable JS in their browser to bipass your validation checking...
|
|
|
Post by Prostyler on May 14, 2008 6:54:21 GMT
That's absolutely fine Dalton, And thanks Daniel, though, I have no idea how to validate in PHP either Learning as I'm shown with programming, but it's a good point that they can disable java. I would still like to validate in Java and see how it goes. If we're getting junk through still, then I will have to come back to find a validation in PHP instead, but if you're willing to help me with this, then fantastic! Thanks for all the work Dalton, and good luck with the exams.
|
|
|
Post by Dalton on May 21, 2008 14:06:45 GMT
Alright. An example of a form that isnt email or postcode should be done like this.
<script> function validate_form ("INSERTFORMNAMEHERE" ) valid = true if ( document.contact_form.contact_name.value == "" ) alert ( "Please fill in the (Form Name)." ); valid = false; return valid; </script>
You can find the name in the code here
<input type="text" name="FORMNAME" />
If you want the serverside validation just post back. Sorry I took so long.
|
|
|
Post by Prostyler on Jun 4, 2008 15:01:27 GMT
Hi again Dalton! I'm still struggling with this validation, so I've just left it for now while developing the rest of the site, however I am now back to try and sort it! I'll show you everything I have in code now, including the form and the php. HTML Form <form method="POST" name="enquiryForm" action="contact.php"> <table cellpadding="0" cellspacing="0" id="contacttable"> <tr class="trblue"> <td>Contact Name</td> <td><input type="text" name="Name" /></td> </tr> <tr> <td>Email Address</td> <td><input type="text" name="EmailFrom" /></td> </tr> <tr class="trblue"> <td>Contact Number</td> <td><input type="text" name="ContactNumber" /></td> </tr> <tr> <td>Enquiry Details<br/> <span class="enquiryextra">If you have an enquiry about more than one product, please provide details for these.</span></td> <td><textarea name="EnquiryDetails"></textarea></td> </tr> <tr class="trblue"> <td>Required Quantity</td> <td><input type="text" name="RequiredQuantity" /></td> </tr> <tr> <td>No. Of Print Colours</td> <td><input type="text" name="NoOfPrintColours" /></td> </tr> <tr class="trblue"> <td>Request A Sample</td> <td><input type="checkbox" name="RequestASample" value="Yes" class="checkbox" /></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" name="submit" value="Send Enquiry" class="submit"/></td> </tr> </table> </form> PHP<?php
// get posted data into local variables $EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); $EmailTo = "sales@premier-gifts.co.uk, njarrett@premier-gifts.co.uk"; $Subject = "Enquiry"; $Name = Trim(stripslashes($_POST['Name'])); $ContactNumber = Trim(stripslashes($_POST['ContactNumber'])); $CompanyName = Trim(stripslashes($_POST['CompanyName'])); $EnquiryDetails = Trim(stripslashes($_POST['EnquiryDetails'])); $RequiredQuantity = Trim(stripslashes($_POST['RequiredQuantity'])); $NoOfPrintColours = Trim(stripslashes($_POST['NoOfPrintColours'])); $RequestASample = Trim(stripslashes($_POST['RequestASample']));
// validation $validationOK=true; if (Trim($EmailFrom)=="") $validationOK=false; if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit; }
// prepare email body text $Body = ""; $Body .= "Contact Name: "; $Body .= $Name; $Body .= "\n"; $Body .= "Email Address: "; $Body .= $EmailFrom; $Body .= "\n"; $Body .= "Contact Number: "; $Body .= $ContactNumber; $Body .= "\n"; $Body .= "Enquiry Details: "; $Body .= $EnquiryDetails; $Body .= "\n"; $Body .= "Required Quantity: "; $Body .= $RequiredQuantity; $Body .= "\n"; $Body .= "No Of Print Colours: "; $Body .= $NoOfPrintColours; $Body .= "\n"; $Body .= "Request A Sample: "; $Body .= $RequestASample; $Body .= "\n";
// send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page if ($success){ print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">"; } else{ print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; } ?> At the minute, I understand that there is a very small validation within the PHP already, set on the email field, so that there MUST be something, anything, entered in that field, otherwise it does not send successfully, but instead, goes through to an error.htm page which says they have not entered a correct email address. I want to validate the email field as best as possible, so that it must be a valid email address, and the domain name provided must exist etc. I appreciate your help so far so much, and hope that you will help me further with this. Thanks alot, Ricky
|
|
|
Post by Dalton on Jun 6, 2008 23:54:51 GMT
<?php
// get posted data into local variables $EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); $EmailTo = "sales@premier-gifts.co.uk, njarrett@premier-gifts.co.uk"; $Subject = "Enquiry"; $Name = Trim(stripslashes($_POST['Name'])); $ContactNumber = Trim(stripslashes($_POST['ContactNumber'])); $CompanyName = Trim(stripslashes($_POST['CompanyName'])); $EnquiryDetails = Trim(stripslashes($_POST['EnquiryDetails'])); $RequiredQuantity = Trim(stripslashes($_POST['RequiredQuantity'])); $NoOfPrintColours = Trim(stripslashes($_POST['NoOfPrintColours'])); $RequestASample = Trim(stripslashes($_POST['RequestASample']));
// validation function check_email_address($email) if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) $email_array = explode("@", $email); $local_array = explode(".", $email_array[0]); for ($i = 0; $i < sizeof($local_array); $i++) if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) return false; if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) $domain_array = explode(".", $email_array[1]); if (sizeof($domain_array) < 2) return false; for ($i = 0; $i < sizeof($domain_array); $i++) if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) return false; return true; } }
// prepare email body text $Body = ""; $Body .= "Contact Name: "; $Body .= $Name; $Body .= "\n"; $Body .= "Email Address: "; $Body .= $EmailFrom; $Body .= "\n"; $Body .= "Contact Number: "; $Body .= $ContactNumber; $Body .= "\n"; $Body .= "Enquiry Details: "; $Body .= $EnquiryDetails; $Body .= "\n"; $Body .= "Required Quantity: "; $Body .= $RequiredQuantity; $Body .= "\n"; $Body .= "No Of Print Colours: "; $Body .= $NoOfPrintColours; $Body .= "\n"; $Body .= "Request A Sample: "; $Body .= $RequestASample; $Body .= "\n";
// send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
?>
You know what this is. TTYL
|
|
|
Post by Prostyler on Jun 7, 2008 8:58:14 GMT
Hi Dalton, I've just tested this, and I get this when submitting: Parse error: syntax error, unexpected T_IF, expecting '{' in /home/prostyle/public_html/validation_test/contact.php on line 17 Any ideas? Thanks
|
|
|
Post by Virtuoso on Jun 17, 2008 19:06:00 GMT
He has been ripping codes from different pages and plastering them together, which is why they don't work. Do you need this done still as it's an old request?
|
|