Jump to content

Form validation - available username checker?


pinacoladaxb

Recommended Posts

Use something like:

 

<?php

$username = mysql_real_escape_string($_POST['username']);

$check = mysql_query("SELECT userField FROM yourUserTable WHERE userField = '{$username}'");
if (mysql_num_rows($check) > 0) {
    print 'Username taken!';
} else {
    print 'Username available!';
}

?>

 

Adam

Link to comment
Share on other sites

I have a register.php page that checks the username against the database and returns a value of Your username is already in use. Please select another if already in use. This is my php, in case it helps:

 

<?php
session_start();
// Connect to MySQL database:
$access = mysql_connect('localhost','root','') or die ('Could not connect to database');
mysql_select_db('smrpg',$access) or die ('Could not select table');
# #
$error = array();
if(isset($_POST['username'])) {
$result = @mysql_query('SELECT username FROM `users` WHERE username = \''.mysql_real_escape_string($_POST['username']).'\'');
if($row = @mysql_fetch_row($result)) {
array_push($error, 'Your username is already being used. Please select another.');
}
$len = strlen($_POST['username']);
if($len < 3 || ($len > 20)) { array_push($error, 'Your username must be between 3 and 20 characters long.'); }
$len = strlen($_POST['password']);
if($len < 6 || ($len > 20)) { array_push($error, 'Your password must be between 6 and 20 characters long.'); }
$len = strlen($_POST['name']);
if($len > 100) { array_push($error, 'Sorry, your name can be no longer than 100 characters long.'); }
if(!$_POST['name']) { array_push($error, 'You must provide your name'); }
if(preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i', $_POST['email']) == false) {
array_push($error, 'Your e-mail address is incorrect');
}
$len = strlen($_POST['email']);
if($len > 255) { array_push($error, 'Sorry, your e-mail address is too long.'); }
if(!$error) {
@mysql_query('INSERT INTO `users` (username, password, name, email) VALUES (\''.mysql_real_escape_string($_POST['username']).'\', \''.mysql_real_escape_string(md5($_POST['password'])).'\', \''.mysql_real_escape_string($_POST['name']).'\', \''.mysql_real_escape_string($_POST['email']).'\')');
header('Location: login.php');
die('<a href="login.php">Login</a>');
}
}
?>

Link to comment
Share on other sites

he can do what he want with javascript only .... but it isnt soo good

you can retrive all usernames with PHP and use them into array in JS after that check the inserted username with arrays this works but its not recomended at all.if the list is huge than it can take a while..

so i dont think its a good ideea

Link to comment
Share on other sites

oh aye.. must have skimmed over it ???

 

Well as fireball5 said, AJAX is the answer.. but the bit of code I showed you before with a few small changes will function as the PHP side of it...

 

<?php
// connect

$username = mysql_real_escape_string($_POST['username']);

$check = mysql_query("SELECT userField FROM yourUserTable WHERE userField = '{$username}'");
if (mysql_num_rows($check) > 0) {
   print 1;
} else {
   print 0;
}

?>

 

Then just follow a tutorial such as http://www.w3schools.com/Ajax/ajax_database.asp .. to setup the javascript side of it.

 

Any help?

 

Adam

Link to comment
Share on other sites

I've heard it better, if you're even going to use JavaScript to validate, to still use PHP to validate on the server end because JavaScript only validates on the user end and they can disable it as well. Covering both ends seems to work better than just the JavaScript.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.