robert.access Posted October 16, 2009 Share Posted October 16, 2009 hi all! I have search on google on how to enter in a test box only caracters A-Z,a-z,0-9 using php, but all the solutions are not for me I need, if you can help me to allow a user to insert only those caracters and with no space at all, like in username field. Can you please help me with an example? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/177943-text-box-alphanumeric-caracters-only/ Share on other sites More sharing options...
Psycho Posted October 16, 2009 Share Posted October 16, 2009 If you want to prevent the user from actually entering the other characters then you need to use JavaScript. But, you will still want a PHP implementation to handle it in case someone has JS turned off. For the PHP implementation you can use ctype_alnum(): http://us2.php.net/manual/en/function.ctype-alnum.php Quote Link to comment https://forums.phpfreaks.com/topic/177943-text-box-alphanumeric-caracters-only/#findComment-938212 Share on other sites More sharing options...
robert.access Posted October 16, 2009 Author Share Posted October 16, 2009 thank you very much for quick answer. Quote Link to comment https://forums.phpfreaks.com/topic/177943-text-box-alphanumeric-caracters-only/#findComment-938219 Share on other sites More sharing options...
robert.access Posted October 16, 2009 Author Share Posted October 16, 2009 i have found this: <?php $strings = array('AbCd1zyZ9', 'foo!#$bar'); foreach ($strings as $testcase) { if (ctype_alnum($testcase)) { echo "The string $testcase consists of all letters or digits.\n"; } else { echo "The string $testcase does not consist of all letters or digits.\n"; } } ?> I asume that will be in the top of my php page, but what to put on imput form??? <input type="text" name="name" size="40" /> Can you please give me a small example??? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/177943-text-box-alphanumeric-caracters-only/#findComment-938234 Share on other sites More sharing options...
Psycho Posted October 16, 2009 Share Posted October 16, 2009 I'm not understanding what you want. Are you wanting javascript code for the input form? If so, post in the JavaScript forum. Quote Link to comment https://forums.phpfreaks.com/topic/177943-text-box-alphanumeric-caracters-only/#findComment-938278 Share on other sites More sharing options...
teynon Posted October 16, 2009 Share Posted October 16, 2009 This post needs to be moved to javascript. robert.access, it appears you are misunderstanding how PHP works. PHP is a server side programming language. This means that once the page is sent to the viewer, you can no longer control it. That is where javascript comes in. You need to ask this question in javascript. If you are looking for a verification in PHP after the user has submitted the form. You should use a line like this: if (preg_match("@^[A-Z0-9]+$@i", $_POST['myField'])) { // Do this } else { // Fail string does not match. } Quote Link to comment https://forums.phpfreaks.com/topic/177943-text-box-alphanumeric-caracters-only/#findComment-938280 Share on other sites More sharing options...
Psycho Posted October 16, 2009 Share Posted October 16, 2009 This post needs to be moved to javascript. robert.access, it appears you are misunderstanding how PHP works. PHP is a server side programming language. This means that once the page is sent to the viewer, you can no longer control it. That is where javascript comes in. You need to ask this question in javascript. If you are looking for a verification in PHP after the user has submitted the form. You should use a line like this: if (preg_match("@^[A-Z0-9]+$@i", $_POST['myField'])) { // Do this } else { // Fail string does not match. } Teynon, did you even read the thread before posting? In my first response I already explained the difference between JavaScript validation and PHP validation, AND I provided a more efficient solution for PHP validation than using regular expression. In my last post I asked for clarification on what he wanted to determine if it was JS or PHP he was asking about and instructed him to create a new thread in JS if that is what he needed. No need to move this thread since only PHP solutions have been dicussed. Quote Link to comment https://forums.phpfreaks.com/topic/177943-text-box-alphanumeric-caracters-only/#findComment-938285 Share on other sites More sharing options...
teynon Posted October 16, 2009 Share Posted October 16, 2009 mjdamato, yes I did and clearly based on his second posting, he is referring to javascript, so his question is a javascript question, meaning he needs to post in javascript. Quote Link to comment https://forums.phpfreaks.com/topic/177943-text-box-alphanumeric-caracters-only/#findComment-938288 Share on other sites More sharing options...
robert.access Posted October 23, 2009 Author Share Posted October 23, 2009 hi all ant thanks for help. sorry for long time that I dont read this post, but trying to find a solution. So, like mjdamato says, if can I use php it's more convenient. I know verry little about javascript and php. What I need it's <form action....><imput type="text" name="x"></form> so, in the form the user to not be able to copy paste, to not be able to enter !@#$%^&*()_+={}[];':",./<>? etc, just only alpha and numbers. And also, into another form I want to limit the numbers they enter like: <imput type=text maxlength="1"> so user can imput from 0 to 9 but how can I limit this to only 5 from 0 to 5?? Thank you for your help. An example on how to implement this it will verry verry usefull to figure out how to implement to my site. Thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/177943-text-box-alphanumeric-caracters-only/#findComment-942708 Share on other sites More sharing options...
cags Posted October 23, 2009 Share Posted October 23, 2009 If you want to prevent the user from actually entering the other characters then you need to use JavaScript. But, you will still want a PHP implementation to handle it in case someone has JS turned off. For the PHP implementation you can use ctype_alnum(): http://us2.php.net/manual/en/function.ctype-alnum.php As mjdamato already stated if you don't want them to be able to enter the characters you will have to use JavaScript. PHP has no control over the users browser. Quote Link to comment https://forums.phpfreaks.com/topic/177943-text-box-alphanumeric-caracters-only/#findComment-942767 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.