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. Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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; } Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.