Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes you can do by using jQuery as:</p> <pre><code>&lt;script&gt; $(document).ready(function() { $("#username").blur(function() { //remove all the class add the messagebox classes and start fading $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow"); //check the username exists or not from ajax $.post("user_availability.php",{ user_name:$(this).val() } ,function(data) { if(data=='empty') // if username is empty { $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox { //add message and change the class of the box and start fading $(this).html('Empty user id is not allowed').addClass('messageboxerror').fadeTo(900,1); }); } else if(data=='invalid') // if special characters used in username { $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox { //add message and change the class of the box and start fading $(this).html('Sorry, only letters (a-z), numbers (0-9), and periods (.) are allowed.').addClass('messageboxerror').fadeTo(900,1); }); } else if(data=='no') // if username not avaiable { $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox { //add message and change the class of the box and start fading $(this).html('User id already exists').addClass('messageboxerror').fadeTo(900,1); }); } else { $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox { //add message and change the class of the box and start fading $(this).html('User id available to register').addClass('messageboxok').fadeTo(900,1); }); } }); }); }); &lt;/script&gt; &lt;input type="text" id="username" name="username"/&gt;&lt;span id="msgbox" style="display:none"&gt;&lt;/span&gt; </code></pre> <p>and script for your <strong>user_availability.php</strong> will be:</p> <pre><code>&lt;?php include'includes/config.php'; //value got from the get method $user_name = trim($_POST['user_name']); if($user_name == ''){ echo "empty"; }elseif(preg_match('/[\'^£$%&amp;*()}{@#~?&gt;&lt;&gt;,|=_+¬-]/', $user_name)){ echo "invalid"; }else{ $select = mysql_query("SELECT user_id FROM staff"); $i=0; //this varible contains the array of existing users while($fetch = mysql_fetch_array($select)){ $existing_users[$i] = $fetch['user_id']; $i++; } //checking weather user exists or not in $existing_users array if (in_array($user_name, $existing_users)) { //user name is not availble echo "no"; } else { //user name is available echo "yes"; } } ?&gt; </code></pre> <p>I tried to add for <strong>/</strong> and <strong>\</strong> but not succeeded.</p> <hr> <p>You can also do it by using javascript &amp; code will be:</p> <pre><code>&lt;!-- Check special characters in username start --&gt; &lt;script language="javascript" type="text/javascript"&gt; function check(e) { var keynum var keychar var numcheck // For Internet Explorer if (window.event) { keynum = e.keyCode; } // For Netscape/Firefox/Opera else if (e.which) { keynum = e.which; } keychar = String.fromCharCode(keynum); //List of special characters you want to restrict if (keychar == "'" || keychar == "`" || keychar =="!" || keychar =="@" || keychar =="#" || keychar =="$" || keychar =="%" || keychar =="^" || keychar =="&amp;" || keychar =="*" || keychar =="(" || keychar ==")" || keychar =="-" || keychar =="_" || keychar =="+" || keychar =="=" || keychar =="/" || keychar =="~" || keychar =="&lt;" || keychar =="&gt;" || keychar =="," || keychar ==";" || keychar ==":" || keychar =="|" || keychar =="?" || keychar =="{" || keychar =="}" || keychar =="[" || keychar =="]" || keychar =="¬" || keychar =="£" || keychar =='"' || keychar =="\\") { return false; } else { return true; } } &lt;/script&gt; &lt;!-- Check special characters in username end --&gt; &lt;!-- in your form --&gt; User id : &lt;input type="text" id="txtname" name="txtname" onkeypress="return check(event)"/&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload