SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Albany, N.Y.
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    validate for null and empty(spacebar)

    I am validating my form to see if a user didn't enter data in a text box.
    The code below works if they don't enter data, but if they click in the box and press the spacebar it does not work. I want to test for both empty and null(which is what I think the spacebar is).

    I know I have to change this line to test for both, but I am lost on how to appcomplish this. any ideas?



    if ( document.test.elements[err[i]].value == "" )


    Thanks

    Code:
    <script type='text/javascript'>
    function isEmpty()
    {
    
    	var errmsg = new String();
    	var err = new Array("req1");
    
    	
    	for(i=0;i<err.length;i++) {
    
    		if ( document.test.elements[err[i]].value == "" )
    		{
    			errmsg += "Error";
            	
    		}
    
    	}
    
    	if(errmsg.length > 0) {
    		alert(errmsg);
    		return false;
    	}
    
    	return true;
    }
    
    </script>

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    "Spacebar only" is text. It looks empty to the eye, but as far as computers are concerned, spaces are text.

    Try this:
    Code:
    <script type="text/javascript">
    function isEmpty()
    {
    
    	var errmsg = '', curerr;
    	var err = new Array("req1");
    
    	
    	for(i=0;i<err.length;i++) {
               curerr = document.test.elements[err[i]].replace(/^\s+|\s+$/g, '');
    		if ( curerr.length === 0)
    		{
    			errmsg += "Error";
            	
    		}
    
    	}
    
    	if(errmsg.length > 0) {
    		alert(errmsg);
    		return false;
    	}
    
    	return true;
    }
    
    </script>


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
  •