rocky_88 Posted November 19, 2011 Share Posted November 19, 2011 I'm unable to validate my text box which should contain only alphabets. Here is the code $string=preg_match("/^[a-zA-Z]+$/",$string); if($string) { echo "Valid"; } else { echo "Invalid"; } Even though the textbox contains only alphabets, it shows invalid. I dont know what i'm doing wrong here. However, if I leave a space inside the character class, it validates the string. I've checked the output of the string and there are no spaces what so ever, yet it shows invalid. Quote Link to comment https://forums.phpfreaks.com/topic/251434-validation-of-text-box/ Share on other sites More sharing options...
Pikachu2000 Posted November 19, 2011 Share Posted November 19, 2011 You'd be better with ctype_alpha for this. Quote Link to comment https://forums.phpfreaks.com/topic/251434-validation-of-text-box/#findComment-1289538 Share on other sites More sharing options...
rocky_88 Posted November 19, 2011 Author Share Posted November 19, 2011 @Pikachu2000 Thanks for that, it did the job. Any idea as to what's the error in the code I mentioned? Quote Link to comment https://forums.phpfreaks.com/topic/251434-validation-of-text-box/#findComment-1289556 Share on other sites More sharing options...
Pikachu2000 Posted November 19, 2011 Share Posted November 19, 2011 I don't really know. I just copied/pasted your code, and it seems to work. What values of $string were you were testing it with? Quote Link to comment https://forums.phpfreaks.com/topic/251434-validation-of-text-box/#findComment-1289561 Share on other sites More sharing options...
rocky_88 Posted November 19, 2011 Author Share Posted November 19, 2011 @Pikachu2000 I tried random values. It shows invalid for abcd xyz or anything that I enter. Quote Link to comment https://forums.phpfreaks.com/topic/251434-validation-of-text-box/#findComment-1289565 Share on other sites More sharing options...
Pikachu2000 Posted November 19, 2011 Share Posted November 19, 2011 This code uses your pattern; try it and see what results you get. <?php $strings = array('thisisastring', 'thishas9numbers', 'this has spaces', 'alsoaSTRING', 'special_%ch$arac#ter!s'); foreach( $strings as $string ) { $valid = preg_match("/^[a-zA-Z]+$/",$string); echo "<i>$string</i>"; if($valid) { echo " is Valid"; } else { echo " is Invalid"; } echo '<br>'; } [/code] Quote Link to comment https://forums.phpfreaks.com/topic/251434-validation-of-text-box/#findComment-1289568 Share on other sites More sharing options...
rocky_88 Posted November 19, 2011 Author Share Posted November 19, 2011 This is the output. thisisastring is Valid thishas9numbers is Invalid this has spaces is Invalid alsoaSTRING is Valid special_%ch$arac#ter!s is Invalid For my code, it shows invalid if I use regex and valid if I use ctype_alpha(). Quote Link to comment https://forums.phpfreaks.com/topic/251434-validation-of-text-box/#findComment-1289570 Share on other sites More sharing options...
Pikachu2000 Posted November 19, 2011 Share Posted November 19, 2011 I think I see why you're having issues. If you are assigning the result of preg_match() to the $string variable, it's important to understand what values may be returned from preg_match() . . . preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match_all() on the contrary will continue until it reaches the end of subject. preg_match() returns FALSE if an error occurred. Quote Link to comment https://forums.phpfreaks.com/topic/251434-validation-of-text-box/#findComment-1289573 Share on other sites More sharing options...
rocky_88 Posted November 19, 2011 Author Share Posted November 19, 2011 I'm pretty sure that it does work that way as i'm validating other fields too using regex and they work just fine. Its just with this particular "alphabets only" condition. Quote Link to comment https://forums.phpfreaks.com/topic/251434-validation-of-text-box/#findComment-1289575 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.