SitePoint Sponsor |
|
User Tag List
Results 1 to 2 of 2
-
Jan 24, 2008, 13:02 #1
- 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>
-
Jan 24, 2008, 13:54 #2
- 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