www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] form validation, validateEmail function

  1. #1
    Join Date
    Nov 2010
    Posts
    2

    resolved [RESOLVED] form validation, validateEmail function

    I am having trouble validating with the email field in the form. I believe the problem is in the function validateEmail. I need the email field in the form to check anything less than a full email address, in the format of name@domain.xxx. If I type in dave_brown.@unh and you'll see what I mean. Also, my code isn't testing for blank text boxes,. You can leave out any information on the text box, and the validation will still fall through. Any ideas on how to fix this?

    Code:
    function validateEmail(fld) {
       // var error="";
    		// var email = document.demo.email.value;
        //email = email.replace(/^\s+|\s+$/, '');
    		var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
        // var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    		var emailPattern = /^[_a-zA-Z0-9\\-]+(\.[_a-zA-Z0-9\\-]+)*@[a-zA-Z0-9\\-]+(\.[a-zA-Z0-9\\-]+)*(\.[a-z]{2,3})$/;                           
        var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    		var email = "dongosselin@compuserve.com";
    		
    	/*	
    		if (emailPattern.test(tfld))  {
    			 window.alert("You entered a valid e-mail address.");
    		}
    		else  {
    				 window.alert("You didn't enter a valid e-mail address");
    				 fld.style.background = 'Yellow';  }
     /*		else {
            fld.style.background = 'White';
    				}
    				  */
    			
    		
    		
        if (fld.value == "") {
            fld.style.background = 'Yellow';
            error = "You didn't enter an email address.\n";
        	       
        } else if (emailPattern.test(tfld)) {              
            fld.style.background = 'Yellow';
            error = "Name has to be in name@domain.xxx.\n";
    							
        } else if (fld.value.match(illegalChars)) {    //test email for illegal characters
            fld.style.background = 'Yellow';
            error = "The email address contains illegal characters.\n";
    						
        } else if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@((\w)([\w\-]?)+\.)+(com|net|org)$/i.test(email))) {
    			  fld.style.background = 'Yellow';   
    				error = "Invalid address, top level domain (has to be .com .net or .org).\n";
    		
    		} else {
            fld.style.background = 'White';
        }
        return error;
     
    }

    my whole code:

    http://ceweb.uml.edu/jberg14527/Java...nValidate.html

    can't find the attached file feature, copy and paste from page source to see how it works.

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    function validateEmail(fld) {
        var error=""; // define error
    returns false when incorrect
    Code:
        } else if (!emailPattern.test(tfld)) {              
            fld.style.background = 'Yellow';
            error = "Name has to be in name@domain.xxx.\n";
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Nov 2010
    Posts
    2

    Form Validation, validateEmail

    I replaced emailFilter with emailPattern in function validateEmail and got it to work like a charm. Thank you so much for the help.

    Code:
    function validateEmail(fld) {
        var error="";
    		var email = document.demo.email.value;
        email = email.replace(/^\s+|\s+$/, '');
    		var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    		var emailPattern = /^[_a-zA-Z0-9\\-]+(\.[_a-zA-Z0-9\\-]+)*@[a-zA-Z0-9\\-]+(\.[a-zA-Z0-9\\-]+)*(\.[a-z]{2,3})$/;  
        var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    		
    		
        if (fld.value == "") {
            fld.style.background = 'Yellow';
            error = "You didn't enter an email address.\n";
        	       
        } else if (!emailPattern.test(tfld)) {              
            fld.style.background = 'Yellow';
            error = "Email name has to be in name@domain.xxx format.\n";
    							
        } else if (fld.value.match(illegalChars)) {				//test email for illegal characters
            fld.style.background = 'Yellow';			 					 		
            error = "The email address contains illegal characters.\n";
    						
        } else if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@((\w)([\w\-]?)+\.)+(com|net|org)$/i.test(email))) {
    			  fld.style.background = 'Yellow';   
    				error = "Invalid address, top level domain (has to be .com .net or .org).\n";
    		
    		} else {
            fld.style.background = 'White';
        }
        return error;
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles