Jump to content

Allow only certain ctrs in text box


bulrush

Recommended Posts

If I have a text box for user input like this:

$s='<td><input type="hidden" name="txtMID[]" value="'.$row['mid'].'" size="6" />';
echo "$s\n";

 

How do I allow only certain characters, like letters (upper and lower case) and digits? How do I allow some characters like ampersand, at sign, dollar sign, question mark?

 

Thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/202761-allow-only-certain-ctrs-in-text-box/
Share on other sites

Well, without completely writing the code for you, here's an example of something I wrote that only allows numeric entries:

$s='<td><input type="hidden" name="txtMID[]" value="'.$row['mid'].'" size="6" onkeyup="this.value=getNumeric(this.value);" />';
echo "$s\n";

function getNumeric(text){

if (!isNumeric(text)){
	text = '';
}

return text;
}

function isNumeric(sText){
var ValidChars = "0123456789.-";
var IsNumber=true;
var Char;
for (i = 0; i < sText.length && IsNumber == true; i++){
	Char = sText.charAt(i); 
	if (ValidChars.indexOf(Char) == -1){
		IsNumber = false;
	}
}
return IsNumber;
}

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.