Daney11 Posted December 5, 2007 Share Posted December 5, 2007 Hey guys, Im using this if (eregi ('^[[:alpha:]\.\'\-]{2,15}$', stripslashes(trim($_POST['member_firstname'])))) { However, users are allowed to enter as many spaces as they want, example " Da N e" I want it so that the user can only enter one space in the form, how would i go about doing this? Thanks Quote Link to comment Share on other sites More sharing options...
Daney11 Posted December 5, 2007 Author Share Posted December 5, 2007 and also, to limit it to letters and numbers only? [a-z0-9A-Z] ? Quote Link to comment Share on other sites More sharing options...
magic2goodil Posted December 5, 2007 Share Posted December 5, 2007 You might try something on the actual form page in javascript instead. Something like: <html> <head> <script> var spaces = 0; var theID; function watchKeys(e,id) { theID = id; if(window.event) { // IE key = e.keyCode; } else if(e.which) { // Netscape/Firefox/Opera key = e.which; } if(key == 32) { //spacebar if(spaces == 1) { val = document.getElementById(theID).value; document.getElementById(theID).value = val.substring(0, val.length-1); } else { spaces++; } } } </script> </head> <body> <input type="text" id="myText" name="myText" onkeypress="return watchKeys(event,this.id);"> </body> </html> I tested this a bit after I made it, it's buggy, but it's a start for you if you want to go that route. Quote Link to comment Share on other sites More sharing options...
xyn Posted December 5, 2007 Share Posted December 5, 2007 Hey guys, Im using this if (eregi ('^[[:alpha:]\.\'\-]{2,15}$', stripslashes(trim($_POST['member_firstname'])))) { However, users are allowed to enter as many spaces as they want, example " Da N e" I want it so that the user can only enter one space in the form, how would i go about doing this? Thanks try this... preg_match_all('/\s/', $string, $matches, PREG_OFFSET_CAPTURE, 3); if(!preg_match("|^[a-z0-9]$|i",$string)||count($matches)>1){ print "You have exeeded 1 space, or your string is not alpha-numerical."; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.