Ashish Sood Posted December 10, 2012 Share Posted December 10, 2012 Hi All, I am creating a form, in this form i want user to enter only string in the text box for that can i create a funtion in php which check the value of the text box Or is there is any other alternative to limit the user to enter only string in the text box. Thanks in advance Link to comment Share on other sites More sharing options...
justsomeguy Posted December 10, 2012 Share Posted December 10, 2012 Strings are the only things anyone can enter, all form data is sent as a string. A form submission does not include any data type, just text. Link to comment Share on other sites More sharing options...
Ashish Sood Posted December 10, 2012 Author Share Posted December 10, 2012 yes you are right Sir, But i dont want user to enter any no.[0-9],if some try to enter any no and click on submit button, then error should be display like "invalid input type", hope you understand what i want. Link to comment Share on other sites More sharing options...
satishpoul Posted December 10, 2012 Share Posted December 10, 2012 check thishttp://www.htmlcodetutorial.com/forms/index_famsupp_158.html Link to comment Share on other sites More sharing options...
justsomeguy Posted December 10, 2012 Share Posted December 10, 2012 You can use regular expressions to filter based on whatever rules you want to use. preg_match will check for a match for a pattern in the value you want to check: http://www.php.net/manual/en/function.preg-match.phphttp://www.php.net/manual/en/reference.pcre.pattern.syntax.php If you want to look for anything but letters, then you can use a pattern like this: #[^a-z]#i That will look for any character that is not a letter. Link to comment Share on other sites More sharing options...
birbal Posted December 10, 2012 Share Posted December 10, 2012 youc an also check into http://php.net/ctype 1 Link to comment Share on other sites More sharing options...
Ingolme Posted December 10, 2012 Share Posted December 10, 2012 check thishttp://www.htmlcodet...amsupp_158.html I read this and knew that the code would be pretty bad:// copyright 1999 Link to comment Share on other sites More sharing options...
Ashish Sood Posted December 11, 2012 Author Share Posted December 11, 2012 (edited) Thanks for your replyi tried below code and its working for me.But the only problem is that if my registration form contain 10 text box then i have to check it again and again, can it be possible to create a function and pass the value this function if yes, please give me a hint..<?php if(isset($_POST['submit'])){ $username=$_POST['username']; $pattern= "/[a-z]/"; if(preg_match($pattern,$username)) { echo "OK"; } else { echo "Not ok "; }}?> Thanks In Advance Edited December 11, 2012 by Ashish Sood Link to comment Share on other sites More sharing options...
satishpoul Posted December 11, 2012 Share Posted December 11, 2012 you have not completed bracket or missing ')' in line of if condition.your lineif(preg_match($pattern,$username) make itif(preg_match($pattern,$username)) Link to comment Share on other sites More sharing options...
Ashish Sood Posted December 11, 2012 Author Share Posted December 11, 2012 you have not completed bracket or missing ')' in line of if condition.your lineif(preg_match($pattern,$username) make itif(preg_match($pattern,$username)) Yes sir i find my error and edit my post again please check it. Link to comment Share on other sites More sharing options...
satishpoul Posted December 11, 2012 Share Posted December 11, 2012 yes you can do that using foreach loop foreach($_POST as $pro){ if($pro == 'submit') break; //here $_POST contain your submitted data like username so // use here your function i m using echo echo $pro; } try this Link to comment Share on other sites More sharing options...
Ashish Sood Posted December 11, 2012 Author Share Posted December 11, 2012 (edited) Thanks for the reply i appriciate your efforts @satishpoul Sir could you please apply the function in my code below, so it will more helpfull to understand.and also please tell me how can i call the function . .<?php if(isset($_POST['submit'])) { $username=$value; //$username=$_POST['username']; $pattern= "/[a-z]/"; if(preg_match($pattern,$username)) { echo "OK"; } else { echo "Not ok "; } } ?> <form action="sample.php" method="post"> <fieldset><legend> USER LOGIN</legend> <p><div><label for="username">*USERNAME:</label><input type="text" name="username" value="<?php if(isset($_POST['username'])){ echo htmlentities($_POST['username']); } ?>"/><br /><p></div> <p><div><label for="username">*PASSWORD:</label><input type="password" name="password" /></div></p> <p><div><label for="username">*FIRSTNAME:</label><input type="text" name="username" value="<?php if(isset($_POST['username'])){ echo htmlentities($_POST['username']); } ?>"/><br /><p></div> <p><div><label for="username">*LASTNAME:</label><input type="text" name="username" value="<?php if(isset($_POST['username'])){ echo htmlentities($_POST['username']); } ?>"/><br /><p></div> <input type="submit" name="submit" /></div></p> Edited December 11, 2012 by Ashish Sood Link to comment Share on other sites More sharing options...
satishpoul Posted December 11, 2012 Share Posted December 11, 2012 (edited) try this one.. no need to use function validation_string($value) directly it will check isset submit means you have submitted value and then it will check for the string. <?php if(isset($_POST['submit'])) { //$username=$_POST['username']; $pattern= "/[a-z]/"; foreach($_POST as $value) { if($value == 'submit') break; if(preg_match($pattern,$value)) { echo $value."OK"; } else { echo $value."Not ok "; } } } ?> <form action="sample.php" method="post" action=""> <fieldset><legend> USER LOGIN</legend> <p><div><label for="username">*USERNAME:</label><input type="text" name="username" value="<?php if(isset($_POST['username'])){ echo htmlentities($_POST['username']); } ?>"/><br /><p></div> <p><div><label for="username">*PASSWORD:</label><input type="password" name="password" /></div></p> <p><div><label for="username">*FIRSTNAME:</label><input type="text" name="firstname" value="<?php if(isset($_POST['firstname'])){ echo htmlentities($_POST['firstname']); } ?>"/><br /><p></div> <p><div><label for="username">*LASTNAME:</label><input type="text" name="lastname" value="<?php if(isset($_POST['lastname'])){ echo htmlentities($_POST['lastname']); } ?>"/><br /><p></div> <input type="submit" name="submit" /></div></p> Edited December 11, 2012 by satishpoul Link to comment Share on other sites More sharing options...
justsomeguy Posted December 11, 2012 Share Posted December 11, 2012 You should use continue in that loop instead of break. If you find the submit button you want to skip it and go to the next one, not end the loop. Also, with that regular expression pattern you're just checking if any letter appears in the text, you're not checking if the text is only letters. That regular expression will match if the text contains 1 or more letters, plus anything else. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now