bulrush Posted May 24, 2010 Share Posted May 24, 2010 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 More sharing options...
F1Fan Posted May 24, 2010 Share Posted May 24, 2010 You'll need to either use JavaScript to validate before the page is submitted, or PHP to do it after the fact. Link to comment https://forums.phpfreaks.com/topic/202761-allow-only-certain-ctrs-in-text-box/#findComment-1062672 Share on other sites More sharing options...
bulrush Posted May 24, 2010 Author Share Posted May 24, 2010 How do I do this with Javascript? Link to comment https://forums.phpfreaks.com/topic/202761-allow-only-certain-ctrs-in-text-box/#findComment-1062678 Share on other sites More sharing options...
F1Fan Posted May 24, 2010 Share Posted May 24, 2010 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 https://forums.phpfreaks.com/topic/202761-allow-only-certain-ctrs-in-text-box/#findComment-1062682 Share on other sites More sharing options...
bulrush Posted May 24, 2010 Author Share Posted May 24, 2010 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 https://forums.phpfreaks.com/topic/202761-allow-only-certain-ctrs-in-text-box/#findComment-1062684 Share on other sites More sharing options...
F1Fan Posted May 24, 2010 Share Posted May 24, 2010 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 https://forums.phpfreaks.com/topic/202761-allow-only-certain-ctrs-in-text-box/#findComment-1062690 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.