473,804 Members | 3,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

validate username

nathj
938 Recognized Expert Contributor
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 2032
ronnil
134 Recognized Expert New Member
try this query

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

(note that i put single quotationmarks around the variable

another to check would be to just select everything WHERE username='yourv ar' 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 Recognized Expert Contributor
try this query

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

(note that i put single quotationmarks around the variable

another to check would be to just select everything WHERE username='yourv ar' 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 Recognized Expert Expert
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 Recognized Expert Contributor
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
3866
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 script will continue to run otherwise it will stop. Below you will find my code I've programmed until now, at this moment I've got one big problem: the vbs part of the script works fine if you run it as a vbs file. If I copy/paste it into an ASP...
3
14524
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 how? I can get the hashed value of the password from DBA_USERS, of course, but is there a way to hash old_password to see if it matches? (I wouldn't be
4
7741
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 else other than determine if the credentials are valid. Any pointers would be appreciated.
2
7599
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 I found a simple way and here it is: using System.DirectoryServices; public bool Win2kCredentialsIsValid(string domain, string username, string password) { bool validLogin = false; string adPath = "LDAP://" + domain + "/rootDSE";
0
1365
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 script will continue to run otherwise it will stop. Below you will find my code I've programmed until now, at this moment I've got one big problem: the vbs part of the script works fine if you run it as a vbs file. If I copy/paste it into an ASP...
1
1779
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 he should be an admin how can i know that user blonging to some group like admin or guest user
3
1484
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 it possible to let it validate more than one username and password? For example text3 and text4? <form> <p>ENTER USER NAME : <input type="text" name="text2"> </p> <p> ENTER PASSWORD :
2
3658
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 any SQL. I have read in some other places that you can use cfparam or cfqueryparam to stop this but I don't really know how to use them. This is the code: <cfif len(form.username) EQ 0 OR len(form.password) EQ 0> <p>You did not...
5
2643
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 form(eg: Name, Age, Time, etc.) I want to validate each and every variable. So how can I do that? I found this JavaScript.But in this case for each and every variable I have to write a function.Eg:for each and every variable i have to rewrite the...
0
9587
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.