function validate() { var error = false; var name, phone, email, message; name = phone = email = message = 1; var msg = "Please enter the following information properly before submitting:\n"; if(document.Contact_Form.name.value.length < 3){ name = 0; msg += "\n- Name (your name)"; } if(document.Contact_Form.phone.value.length < 5){ subject = 0; msg += "\n- Please enter a valid phone number"; } if(document.Contact_Form.email.value.length < 7 || document.Contact_Form.email.value.indexOf('@') < 1 || document.Contact_Form.email.value.indexOf('.') < 3) { email = 0; msg += "\n- E-mail (your valid e-mail address)"; } if(document.Contact_Form.message.value.length < 7){ name = 0; msg += "\n- Message (your message, query or comments)"; } if(name + phone + email + message < 4){ alert(msg); return false; } return true; } PHP: in the code i need to add validation the email address properly, like someone needs to type at least 2 or 3 letter of the the (dot), what will be the code? i am trying something || document.Contact_Form.email.value.indexOf(" ")!= < 2 but not working, please give me the exact code...
Normally, regular expressions are used to validate form data. Just don't ask me how they work! I still don't have any idea. http://www.regular-expressions.info/email.html <html> <head> <title>Email Validation Demo</title> <script type="text/javascript"> function validate() { var email = document.getElementById("emailbox").value; var result = email.match(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/); if (result != null) alert('Valid email'); else alert('Invalid email'); } </script> </head> <body> <p>Enter your email</p> <p><input id="emailbox" type="text" ></p> <p><button onclick="validate()">Check Email</button></p> </body> </html> Code (markup):