Jump to content

How do I limit my ctype_alnum validation


Recommended Posts

Am Creating a login Script and i do not want users to put in number or alpha numeric characters in the username field and i use the the ctype_alnum function the problem is that the validation is also affecting the password field below is login script.

<div id="login">
<?php if(isset($_POST["login"])&&(empty($_POST["username"])||empty($_POST["password"])))
{
echo"<p class='style1' style='background:yellow;' >
Please You Have to Provide a Valid Username and password</p>";
$staff=false;}?>
<form action="<?php $_SERVER["PHP_SELF"] ?>" method="post">
<label style="font-style:bold;color:#FF9900;" ><h2>UserName:</h2> </label>
<?php if(isset($_POST["login"])&&(empty($_POST["username"])))
{
echo"Your User Name is Required";$staff=false;
}
elseif(ctype_alnum($_POST["username"])==true)
{
echo"You are allowed to put in Numbers<br>"; $staff=false;
}
?>
<input size="40" type="text" name="username" placeholder="UserName"/><br><br>
<label style="font-style:bold;color:#FF9900;" ><h2>Password:</h2> </label>
<?php if(isset($_POST["login"])&&(empty($_POST["password"])))
{
echo"Your Password is Required";$staff=false;
}			
?>
<input style="radius:6;" size="40" type="password" placeholder="Password" name="password"/><br></div>
<input type="submit" width="25px" value="Login" name="login"/>
</form>

so please help check the code for errors and how to limit the ctype_alnum validation to just the username field.

i do not want users to put in number or alpha numeric characters in the username field

 

Then what characters do you want them to use? You know alpha-numeric covers A-Z and 0-9 right? So if you disallow it you restrict people to international or symbol characters.

 

I'm going to run on the assumption you mean you want users to only use alpha-numeric characters, in which case you'd do:

if (!ctype_alnum($_POST['username'])){
   echo "You may only use alpha-numeric characters";
   $staff=false;
}
If you want the same restriction on the password, you'd do the same check. If you don't want the restriction then do nothing. Right now your only restriction on the password is that it is not empty.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.