Jump to content
xisto Community
Sign in to follow this  
rr77

Username Validation With Php

Recommended Posts

A simple method of validation. This is good if you dont want spaces between caracters:

 

form.php

 

<FORM action="vali.php" method="post">	<P>	<LABEL for="user">User name: </LABEL>	<INPUT type="text" id="user"><BR>	<INPUT type="submit" value="Send"> 	</P></FORM>

vali.php

 

<?php$user =$_POST['user']; $cuser=0;for($i=0; $i<strlen($user); $i++)   { if ($user[$i]==" ")	{   $cuser=1;	}  }  if($cuser==1){  echo "The user name have spaces";}else{   echo "The user name is correct";}?>

Notice from rvalkass:

Remember, all code must be placed in CODE BB tags. Thank you.


Share this post


Link to post
Share on other sites
<? if(preg_match("/ /", $_POST['user'])){  echo "The user name have spaces"; }else{  echo "The user name is correct"; }?>

This is exactly the same result in way shorter code. Besides to remove the spaces and to set the username and password to lowercase [when you have case-insensitive login system] and to prepare them for inserting to the database just use this code:

<?$username = htmlentities(strtolower(trim($_POST['user'])),ENT_QUOTES);$password  = htmlentities(strtolower(trim($_POST['password'])),ENT_QUOTES);?>

Share this post


Link to post
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
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.