Jump to content

Accept only string in PHP


Ashish Sood

Recommended Posts

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

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 by Ashish Sood
Link to comment
Share on other sites

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 by Ashish Sood
Link to comment
Share on other sites

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 by satishpoul
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...