SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot frosco's Avatar
    Join Date
    Jul 2003
    Location
    WA state
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question IMA NEWBIE - JS Form Validation Problem

    For some reason my form is letting blank email addresses slip by. It will validate them if they're input incorrectly, but does nothing if the string is <2 characters.

    I have 1% idea what I'm doing with JS..please help me gently. =)

    Code:
    function test(src)
    {
    	var emailReg = "^[\\w-_'\.]*[\\w-_'\.]\@[\\]\.+[\\w-_]+[\\w]$";
    	var regex = new RegExp(emailReg);
    	return regex.test(src);
    }
    
    function validate() {
    	var i=0;
    	var errcolour='#CC3300';
    	var msg='The following fields did not pass validation:\n\n';
    
    	if(document.guestbook.EMAIL.value.length >1)
    	{
    		emailValid = test(document.guestbook.EMAIL.value);
    		if(emailValid == false)
    		{
    			++i;
    			msg += "Your E-mail address is not valid.\n";		
    			document.guestbook.EMAIL.style.background=errcolour;
    			document.guestbook.EMAIL.style.color='#FFFFFF';		
    		}else{
    			document.guestbook.EMAIL.style.background='#FFFFFF';
    			document.guestbook.EMAIL.style.color='';				
    		}
    	}
    if(document.guestbook.EMAIL.value.length <2)
    	{
    		++i;
    		msg += 'You must supply a valid email address\n';
    		document.guestbook.EMAIL.style.background=errcolour;
    		document.guestbook.EMAIL.style.color='#FFFFFF';		
    	}else{
    		document.guestbook.EMAIL.style.background='#FFFFFF';
    		document.guestbook.EMAIL.style.color='';				
    	}
    ....there is more to this code, but nothing pertaining the problem ( I don't think!)

    Thanks!

  2. #2
    SitePoint Zealot frosco's Avatar
    Join Date
    Jul 2003
    Location
    WA state
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I figured it out--just had to flip-flop the two if/else statements.


Bookmarks

Posting Permissions

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