Lee-Bartlett Posted October 8, 2008 Share Posted October 8, 2008 I am brand new to java script and im not really sure completly how it all works, but i wanna do a simple email validation, worked in with my php form. Heres the code, where am i going wrong? <?php if(isset($_POST['button'])) { //db connection require_once("includes/db_connection.php"); //end of db connection // db table connection include("includes/form.php"); //end of db table connection } ?> <html> <head> <script type="text/javascript"> function validate_email(email,alerttxt) { with (email) { apos=value.indexOf("@"); dotpos=value.lastIndexOf("."); if (apos<1||dotpos-apos<2) {alert(alerttxt);return false;} else {return true;} } } function validate_form(thisform) { with (thisform) { if (validate_email(email,"Not a valid e-mail address!")==false) {email.focus();return false;} } } </script> <title></title> <style type="text/css"> <!-- .style1 { font-size: 36px; color: #FFFFFF; } --> </style> </head> <body> <table width="617" border="1" align="center" cellpadding="5" cellspacing="0" bordercolor="#000000"> <tr> <td colspan="2" bgcolor="#0099FF"><p> </p> <p align="center" class="style1">Nexodom.com</p> <p> </p></td> </tr> <tr> <td width="129" height="318" align="left" valign="top"><p><a href="index.html">Home</a><br> <a href="wifi.php">WIFI Hot Spots</a></p> </td> <td width="482" align="left" valign="top"><p align="center">Welcome to nexodom.com</p> <form name="form" method="post" action="includes/form.php"> <input type="hidden" name="redirect" value="home.php"> <table width="418" align="left" cellpadding="0" cellspacing="0"> <tr> <td width="157"> Name:</td> <td width="259"><label for="name"></label> <input type="text" name="name" id="name"></td> </tr> <tr> <td>Email:</td> <td><label for="email"></label> <input type="text" name="email" id="email"></td> </tr> <tr> <td>WiFi Business Name:</td> <td><label for="buissnes_name"></label> <input type="text" name="buissnes_name" id="buissnes_name"></td> </tr> <tr> <td>WiFi Location;</td> <td><label for="textfield"></label> <input type="text" name="location" id="location"></td> </tr> <tr> <td>Free or Paid:</td> <td>Free<input type="radio" name="type" id="type" value="free"> <label for="radio"></label> Paid<input type="radio" name="type" id="type" value="paid"> <label for="radio2"></label></td> </tr> <tr> <td> </td> <td><label for="button"></label> <input type="submit" name="button" id="button" value="Submit"> <label for="button2"></label> <input type="reset" name="button2" id="button2" value="Reset"> <label for="sub"></label></td> </tr> </table> </form> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p></td> </tr> <tr> <td height="20" colspan="2" bgcolor="#0099FF"> </td> </tr> </table> </body> </html> <?php mysql_close ?> Quote Link to comment https://forums.phpfreaks.com/topic/127554-validation/ Share on other sites More sharing options...
JonnoTheDev Posted October 8, 2008 Share Posted October 8, 2008 Using Javascript validation for form input is a really bad idea in my opinion. Here are my reasons: 1. Can be easily switched of within the web browser so no user data gets validated leaving a gaping security hole, especially if the input is used with database queries (see SQL injection) 2. If your form sends out email after processing input a bot can easily be written (bots do not parse javascript) to create outbound spam from your server. Quote Link to comment https://forums.phpfreaks.com/topic/127554-validation/#findComment-660004 Share on other sites More sharing options...
obsidian Posted October 8, 2008 Share Posted October 8, 2008 Using Javascript validation for form input is a really bad idea... Yes and no. To solely rely on JavaScript is a bad idea for the reasons provided; however, many users will prefer to be warned of faulty data before they have to leave the page, so using some JavaScript validation in conjunction with PHP validation on the receiving end is definitely a good idea. Now, in answer to the original post, I don't see where you are actually calling the validation on submit. You have the validate_form and validate_email functions defined, but you never attach the validation to the form submit event. The easiest way to do this is to call your function in the onsubmit attribute of the form tag. Remember that if you return FALSE from an onsubmit, the form will not process: <script type="text/javascript"> function my_simple_validation (field) { if (field.value == '') { alert('You must fill in required fields!'); return false; // This is where you tell the form not to submit } return true; // if we pass, submit the form } </script> <form name="my_form" action="process.php" method="post" onsubmit="return my_simple_validation(this.email);"> ... Obviously, this is generic code, but it should give you the principles needed. Good luck! Quote Link to comment https://forums.phpfreaks.com/topic/127554-validation/#findComment-660023 Share on other sites More sharing options...
Lee-Bartlett Posted October 8, 2008 Author Share Posted October 8, 2008 ty im very new to java and only need it so far for validation thx Quote Link to comment https://forums.phpfreaks.com/topic/127554-validation/#findComment-660067 Share on other sites More sharing options...
Lee-Bartlett Posted October 8, 2008 Author Share Posted October 8, 2008 Hmm, it isnt validating the @ sign, it works if nothing is in the box, it goes please fill in box, but i can still do a email as a name, any advice here? Quote Link to comment https://forums.phpfreaks.com/topic/127554-validation/#findComment-660106 Share on other sites More sharing options...
Lee-Bartlett Posted October 8, 2008 Author Share Posted October 8, 2008 Sorry im a complete novice, i cant see where my other validation, for name, if empty i want it to return anything to let me know its working, this is my script so far. <script type="text/javascript"> function validate_required(name,alerttxt) { with (name) { if (name==null||name=="") { alert(alerttxt);return false; } else { return true; } } } function validate_email(email,alerttxt) { with (email) { apos=value.indexOf("@"); dotpos=value.lastIndexOf("."); if (apos<1||dotpos-apos<2) {alert(alerttxt);return false;} else {return true;} } } function validate_form(thisform) { with (thisform) { if (validate_email(email,"Not a valid e-mail address!")==false) {email.focus();return false;} } } function my_simple_validation (email) { if (email.value == '') { alert('You must fill in required fields!'); return false; // This is where you tell the form not to submit } return true; // if we pass, submit the form } </script> <form name="my_form" action="includes/form.php" method="post" onsubmit="return my_simple_validation(this.email);"> </script> Quote Link to comment https://forums.phpfreaks.com/topic/127554-validation/#findComment-660116 Share on other sites More sharing options...
Lee-Bartlett Posted October 8, 2008 Author Share Posted October 8, 2008 Now i tried this, move few stuff about, the @ still isnt validating, and i cant get the name to if blank say a message. <?php //db connection require_once("includes/db_connection.php"); //end of db connection ?> <html> <head> <script type="text/javascript"> function validate_required(name,alerttxt) { with (name) { if (value=="") { alert(alerttxt); return false; } else { return true; } } } function validate_email(email,alerttxt) { with (email) { apos=value.indexOf("@"); dotpos=value.lastIndexOf("."); if (apos<1||dotpos-apos<2) { alert(alerttxt); return false; } else { return true; } } } function validate_form(thisform) { with (thisform) { if (validate_required(email,"Email is a required field")==false) { email.focus(); return false; } else if (validate_email(email,"Not a valid e-mail address!")==false) { email.focus(); return false; } else if (validate_required(name,"Name is a required field")==false) { email.focus(); return false; } } return true; } function my_simple_validation (email) { if (email.value == '') { alert('You must fill in required fields!'); return false; // This is where you tell the form not to submit } return true; // if we pass, submit the form } </script> <?php if(isset($_POST['button'])) { // db table connection include("includes/form.php"); //end of db table connection } ?> <title></title> <style type="text/css"> <!-- .style1 { font-size: 36px; color: #FFFFFF; } --> </style> </head> <body> <form name="form" method="post" action="includes/form.php" onsubmit="return my_simple_validation(this.email);"> <table width="617" border="1" align="center" cellpadding="5" cellspacing="0" bordercolor="#000000"> <tr> <td colspan="2" bgcolor="#0099FF"><p> </p> <p align="center" class="style1">Nexodom.com</p> <p> </p></td> </tr> <tr> <td width="129" height="318" align="left" valign="top"><p><a href="index.html">Home</a><br> <a href="wifi.php">WIFI Hot Spots</a></p> </td> <td width="482" align="left" valign="top"><p align="center">Welcome to nexodom.com</p> <input type="hidden" name="redirect" value="home.php"> <table width="418" align="left" cellpadding="0" cellspacing="0"> <tr> <td width="157"> Name:</td> <td width="259"><label for="name"></label> <input type="text" name="name" id="name"></td> </tr> <tr> <td>Email:</td> <td><label for="email"></label> <input type="text" name="email" id="email"></td> </tr> <tr> <td>WiFi Business Name:</td> <td><label for="buissnes_name"></label> <input type="text" name="buissnes_name" id="buissnes_name"></td> </tr> <tr> <td>WiFi Location;</td> <td><label for="textfield"></label> <input type="text" name="location" id="location"></td> </tr> <tr> <td>Free or Paid:</td> <td>Free<input type="radio" name="type" id="type" value="free"> <label for="radio"></label> Paid<input type="radio" name="type" id="type" value="paid"> <label for="radio2"></label></td> </tr> <tr> <td> </td> <td><label for="button"></label> <input type="submit" name="button" id="button" value="Submit"> <label for="button2"></label> <input type="reset" name="button2" id="button2" value="Reset"> <label for="sub"></label></td> </tr> </table> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p></td> </tr> <tr> <td height="20" colspan="2" bgcolor="#0099FF"> </td> </tr> </table> </form> </body> </html> <?php mysql_close ?> Quote Link to comment https://forums.phpfreaks.com/topic/127554-validation/#findComment-660328 Share on other sites More sharing options...
Psycho Posted October 8, 2008 Share Posted October 8, 2008 Here is my email validation script. It returns true if the string is a properly formatted email, else false. It is pretty comprehensive. I have inlcluded the details of the validation below the script function validEmail(emailStr) { //Return true/false for valid/invalid email formatTest = /^[-\w+]+(\.[-\w+]+)*@[-a-z\d]{2,}(\.[-a-z\d]{2,})*\.[a-z]{2,6}$/i lengthTest = /^(.{1,64})@(.{4,255})$/ return (formatTest.test(emailStr) && lengthTest.test(emailStr)); } Validation Details: The username: Can contain the following characters: 'a-z', 'A-Z', '0-9', '_' (underscore), '-' (dash), '+' (plus sign), and '.' (period). May not begin or end with a period and they may not appear in succession (i.e. 2 or more in a row) Must be between 1 and 64 characters The domain name: Can contain the following characters: 'a-z', 'A-Z', '0-9', '-' (dash), and '.' (period). There may be subdomains, separated by a period (.), but the combined domain may not begin with a period and they not appear in succession (i.e. 2 or more in a row) The 'combined' domain name must be followed by a period and the TLD (top level domain). The TLD (Top Level Domain): Can contain the following characters: 'a-z', and 'A-Z'. The TLD must consist of 2-6 alpha characters in either upper or lower case. (6 characters are needed to support .museum). Quote Link to comment https://forums.phpfreaks.com/topic/127554-validation/#findComment-660375 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.