adding something after dot(.)

Discussion in 'JavaScript' started by me4you, Apr 8, 2011.

  1. #1
    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...
     
    me4you, Apr 8, 2011 IP
  2. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Normally, regular expressions are used to validate form data.
    Just don't ask me how they work! I still don't have any idea. :confused:
    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):
     
    Cash Nebula, Apr 9, 2011 IP