Jump to content

eregi


Daney11

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/80278-eregi/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/80278-eregi/#findComment-406877
Share on other sites

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.";
}

Link to comment
https://forums.phpfreaks.com/topic/80278-eregi/#findComment-406890
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.