By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,320 Members | 2,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,320 IT Pros & Developers. It's quick & easy.

validate username

nathj
Expert 100+
P: 938
Hi,

I'm working on a registration form and one of the checks I need to perform as the form is used is on the username. I need to ensure that it is not already in use. I am getting a little stuck. Here's the code involved (sorry there's quite a bit) and then I'll explain the problem.

HTML (relevant sample)
Expand|Select|Wrap|Line Numbers
  1.     <div class="row">
  2.         <span class="label" id="usernamelabel">
  3.             <span class="warninglabel">Username:</span>
  4.         </span>
  5.         <span class="formw">
  6.             <input id="username" type="text" size="47" onchange="validateItem('usernamelabel',this.value,'Username:',4,false,5,true);" title="Username, for use on the Forum" />
  7.             (6-18 alpha numeric characters)
  8.         </span>
  9.     </div>
  10.  
Javascript (3 functions involved)
Expand|Select|Wrap|Line Numbers
  1. function stateChanged() 
  2.     if (goXMLHTTP.readyState==4 || goXMLHTTP.readyState=="complete")
  3.     { 
  4.     document.getElementById(gcItemID).innerHTML=goXMLHTTP.responseText; 
  5.     } 
  6.  
  7. function GetXmlHttpObject()
  8.     if (window.XMLHttpRequest)
  9.     {
  10.         goXMLHTTP=new XMLHttpRequest()
  11.     }
  12.     else if (window.ActiveXObject)
  13.     {
  14.         goXMLHTTP=new ActiveXObject("Microsoft.XMLHTTP")
  15.     }     
  16.     if (goXMLHTTP==null)
  17.     {
  18.         alert ("Browser does not support HTTP Request")
  19.          return    
  20.     } 
  21.  
  22.  
  23. function validateItem(pcID,pcItem,pcDisplay,pnType,plPopulateExtra,pnNumberOfItems,plCheckDatabase)    
  24. {    
  25.     var lcRegExp, llIsValid, llUseRegExp
  26.     llIsValid = false 
  27.     llUseRegExp = false
  28.     switch (pnType)
  29.     {
  30.         case 1:    // UK postcode
  31.             pcItem = pcItem.toUpperCase()     
  32.             lcRegExp = '^[A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA$' 
  33.             llUseRegExp = true
  34.             break;
  35.         case 2: // email address
  36.             lcRegExp = '^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$'
  37.             llUseRegExp = true            
  38.             break;
  39.         case 3: // number of set length - basic telephone number check
  40.             lcRegExp = '^[0-9]{6,11}$'                                            
  41.             llUseRegExp = true            
  42.             break;
  43.         case 4: // username and password validation - alpha numeric characters 6-18 characters long
  44.             lcRegExp = '^[A-Za-z0-9]{6,18}$' 
  45.             llUseRegExp = true            
  46.             break;                             
  47.     }
  48.  
  49.     if (llUseRegExp)
  50.     {
  51.         if (pcItem.match(lcRegExp))
  52.         {    
  53.             if(plCheckDatabase)
  54.             {    
  55.                 GetXmlHttpObject() // sets the global variable
  56.                 gcItemID = pcID
  57.                 gcUrl = "../lib/datacheck.php?check=1&tocheck='" + pcItem + "'"
  58.                 goXMLHTTP.onreadystatechange=stateChanged 
  59.                 goXMLHTTP.open("GET",gcUrl,true)
  60.                 goXMLHTTP.send(null)
  61.  
  62.             }
  63.             else
  64.             {
  65.                 llIsValid = true
  66.             }
  67.         }
  68.         else
  69.         {              
  70.             llIsValid = false
  71.         }
  72.     }    
  73.     else
  74.     {                 
  75.         if (pcItem == null||pcItem == ""||pcItem.length < 2)
  76.         {
  77.             llIsValid = false
  78.         }
  79.         else
  80.         {
  81.             llIsValid = true
  82.         }     
  83.     }
  84.  
  85.     if (!plCheckDatabase)
  86.     {
  87.         if (llIsValid)                                
  88.         {                
  89.             document.getElementById(pcID).innerHTML = pcDisplay
  90.             if (gnValidationCount >= 1)
  91.             {
  92.                 gnValidationCount = gnValidationCount - 1
  93.             }
  94.         }
  95.         else
  96.         {    
  97.             document.getElementById(pcID).innerHTML = "<span class='warninglabel'>" + pcDisplay + "</span>"
  98.             if (gnValidationCount <= pnNumberOfItems)
  99.             {
  100.                 gnValidationCount = gnValidationCount + 1
  101.             }
  102.         }     
  103.     }
  104.  
  105.     if (gnValidationCount == 0)    
  106.     {
  107.         hideOrShowInput("complete",false)    
  108.     }
  109.     else
  110.     {
  111.         hideOrShowInput("complete",true)    
  112.     }    
  113.     if (plPopulateExtra)
  114.     {
  115.          populateExtra(lcSource, lcDestination, true, true)
  116.     }  
  117. }
  118.  
PHP (the page referenced in validateItem())
Expand|Select|Wrap|Line Numbers
  1. <?php    
  2.  
  3.     // establish what is being checked via the $_REQUEST variable
  4.     $lnCheckType    = $_REQUEST['check'];
  5.     $lcItemToCheck    = $_REQUEST['tocheck'];    
  6.     switch($lnCheckType)
  7.     {
  8.         case 1: // check for presence of username, will only ever be one match
  9.             $lcCheckSQL = 
  10.             "SELECT 1 as test
  11.             FROM credential 
  12.             WHERE username like '$lcItemToCheck'" ; 
  13.  
  14.             $laResult = $loDB->queryGetData($lcCheckSQL); 
  15.  
  16.             foreach($laResult as $lcTest)
  17.             {
  18.                 $llAvailable = $lcTest['test'];
  19.                 if($llAvailable > 0)
  20.                 {
  21.                     echo "<span class='warninglabel'>Username in use:</span>";
  22.                 }
  23.                 else
  24.                 {
  25.                     echo "Username:";
  26.                 }
  27.             }    
  28.             break;    
  29.  
  30.         case 2:// just to show that we can expand this system should we ever need to 
  31.             echo "case = 2";
  32.             break;
  33.     }
  34. ?>
  35.  
The Problem

The php will echo the tow variables from $_REQUEST, and the $lcCheckSQL is defined correctly. However, there appears to be a problem with the way the SQL is executing as all that happens is the label that is supposed to change is blanked.

I believe the connection to the database is available, this is set at the top of the page that has the form on it and so should be available. Even if I add a connection to this page the behaviour is the same.

This is really frustrating now and I'd love some help with this.

Thanks for reading (I know it was long)

nathj
Jun 28 '07 #1
Share this Question
Share on Google+
4 Replies


ronnil
Expert 100+
P: 134
try this query

"SELECT COUNT(*) as test FROM credential WHERE username='$IcItemToCheck'";

(note that i put single quotationmarks around the variable

another to check would be to just select everything WHERE username='yourvar' and test if the $result has any rows in it. If it has, the username or whatever is ofcourse illegal :)
Jun 28 '07 #2

nathj
Expert 100+
P: 938
try this query

"SELECT COUNT(*) as test FROM credential WHERE username='$IcItemToCheck'";

(note that i put single quotationmarks around the variable

another to check would be to just select everything WHERE username='yourvar' and test if the $result has any rows in it. If it has, the username or whatever is ofcourse illegal :)
Thanks for the help, I have further tracked down the trouble with this. If i change the php file to:
Expand|Select|Wrap|Line Numbers
  1. -->        
  2. <?php    
  3.     // establish what is being checked via the $_REQUEST variable
  4.     $lnCheckType    = $_REQUEST['check'];
  5.     $lcItemToCheck    = $_REQUEST['tocheck'];     
  6.  
  7.     //establish connection - using dataobject.php did not work?
  8.     $lvCon = mysql_connect('server', 'user', 'password');
  9.     if (!$lvCon)
  10.     {
  11.         die('Could not connect: ' . mysql_error());
  12.     }
  13.     mysql_select_db("clfdb", $lvCon);
  14.  
  15.     // branch the code
  16.     switch($lnCheckType)
  17.     {
  18.         case 1: // check for presence of username, will only ever be one match or no match
  19.             $lcToDisplay = "Username:";
  20.  
  21.             $lcCheckSQL = 
  22.             "SELECT 1 as test 
  23.             FROM credential 
  24.             WHERE username like $lcItemToCheck" ; 
  25.  
  26.             $result = mysql_query($lcCheckSQL);
  27.  
  28.             while ($row = mysql_fetch_array($result))
  29.             {
  30.                 $lnAvailable = $row['test'];
  31.                 if ($lnAvailable = 1)
  32.                  {
  33.                      $lcToDisplay = "<span class='warninglabel'>Username in use:</span>";
  34.                  }
  35.             }
  36.  
  37.             echo $lcToDisplay;
  38.             break;    
  39.  
  40.         case 2:// just to show that we can expand this system should we ever need to 
  41.             echo "case = 2";
  42.             break;
  43.     }            
  44. ?>
  45.  
it works just fine. This indicates that there is some problem in the way I was attempting to use the data abstraction layer I have developed.

I have changed the SQL to your suggested use of COUNT(*) as this makes more sense when reading. Otherwise I will leave it as it is shown above for the time being and try to figure out a way of not duplicating code later on.

Thanks for the help.
nathj
Jun 28 '07 #3

pbmods
Expert 5K+
P: 5,821
Heya, nathj.

Thanks for using CODE tags! Did you know that you can specify a language for your CODE tags to make your source code easier to read?

You will still need to use [/code] to close your code blocks, regardless of the language, but you can the use one of these tags to open your code block:

[code=html]
[code=javascript]
[code=php]

and so on.

Thanks!

MODERATOR
Jun 28 '07 #4

nathj
Expert 100+
P: 938
Heya, nathj.

Thanks for using CODE tags! Did you know that you can specify a language for your CODE tags to make your source code easier to read?

You will still need to use [/code] to close your code blocks, regardless of the language, but you can the use one of these tags to open your code block:

[code=html]
[code=javascript]
[code=php]

and so on.

Thanks!



MODERATOR
I did not know all that, I will make use of this in future - thanks for the pointer.

I should also say thanks for the help, I have now got this working. The relevant controls validated based on a regular expression and on the database as I wanted them to and the form works a treat.

Many thanks
Nathan
Jun 28 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.