Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.