473,327 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

validate username

nathj
938 Expert 512MB
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
4 2003
ronnil
134 Expert 100+
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
938 Expert 512MB
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
5,821 Expert 4TB
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
938 Expert 512MB
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

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

Similar topics

5
by: Joeri KUMBRUCK | last post by:
Hello, I'm trying to create an ASP page where users can type in a username and password, and these credential should be checked with active directory. If username and password are correct, the...
3
by: arktikturtle | last post by:
Hi! I'm looking for a way to validate a password within PL/SQL. I want to write CREATE PROCEDURE change_password(old_password IN VARCHAR2) IS BEGIN -- check if old_password is correct... but...
4
by: Paul Steele | last post by:
I'm writing a C# program that needs to validate an Active Directory username/password? The program will be running on a workstation that is not part of the domain. It doesn't have to do anything...
2
by: Steffen Balslev | last post by:
I tried to find a way to validate user credentials using C#, searching google and lots of other news and kb sites left me without a solution. You can use a SSPI but it's that easy to implement so...
0
by: Joeri KUMBRUCK | last post by:
Hello, I'm trying to create an ASP page where users can type in a username and password, and these credential should be checked with active directory. If username and password are correct, the...
1
by: Raghuram | last post by:
if i want to validate the LDAP user like i will provide him with the domain name but not the user name and pass when the user enters the user name and pass i should validate towards my domain, and...
3
by: ojnoka | last post by:
Hi, I am a newbie. I don't know javascript. I use the free javascripts and try to understand them. I have a javascript now, to validate username and password. I paste it below. My question now. Is...
2
by: sc0705837 | last post by:
Hi there I am trying to get my action page of the website I'm building to check to see if the form input values are empty, if they are not it will then check to see if any of the form inputs contain...
5
by: ghjk | last post by:
I have a web site with several web pages. They are having different number of variables. eg: first page having 4 variables in the form(Name, Phone, ID, Gender) second page having 10 variables in the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.