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

Link to comment
Share on other sites

In other words I have to use a PHP function which does the brute force method, or perhaps uses a regex?

 

I can't use the pattern attribute? Well, I can't see how the pattern="" attribute would display an error message.

 

Link to comment
Share on other sites

Both of the functions that I listed are JavaScript functions. And, yes, you could use regex to modify what I sent. Or, you could even add what you want as valid characters to the "ValidChars" variable.

Link to comment
Share on other sites

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.