validate username

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    validate username

    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)
    [CODE=html]
    <div class="row">
    <span class="label" id="usernamelab el">
    <span class="warningl abel">Username: </span>
    </span>
    <span class="formw">
    <input id="username" type="text" size="47" onchange="valid ateItem('userna melabel',this.v alue,'Username: ',4,false,5,tru e);" title="Username , for use on the Forum" />
    (6-18 alpha numeric characters)
    </span>
    </div>
    [/CODE]

    Javascript (3 functions involved)
    [CODE=javascript]
    function stateChanged()
    {
    if (goXMLHTTP.read yState==4 || goXMLHTTP.ready State=="complet e")
    {
    document.getEle mentById(gcItem ID).innerHTML=g oXMLHTTP.respon seText;
    }
    }

    function GetXmlHttpObjec t()
    {
    if (window.XMLHttp Request)
    {
    goXMLHTTP=new XMLHttpRequest( )
    }
    else if (window.ActiveX Object)
    {
    goXMLHTTP=new ActiveXObject(" Microsoft.XMLHT TP")
    }
    if (goXMLHTTP==nul l)
    {
    alert ("Browser does not support HTTP Request")
    return
    }
    }


    function validateItem(pc ID,pcItem,pcDis play,pnType,plP opulateExtra,pn NumberOfItems,p lCheckDatabase)
    {
    var lcRegExp, llIsValid, llUseRegExp
    llIsValid = false
    llUseRegExp = false
    switch (pnType)
    {
    case 1: // UK postcode
    pcItem = pcItem.toUpperC ase()
    lcRegExp = '^[A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA$'
    llUseRegExp = true
    break;
    case 2: // email address
    lcRegExp = '^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$'
    llUseRegExp = true
    break;
    case 3: // number of set length - basic telephone number check
    lcRegExp = '^[0-9]{6,11}$'
    llUseRegExp = true
    break;
    case 4: // username and password validation - alpha numeric characters 6-18 characters long
    lcRegExp = '^[A-Za-z0-9]{6,18}$'
    llUseRegExp = true
    break;
    }

    if (llUseRegExp)
    {
    if (pcItem.match(l cRegExp))
    {
    if(plCheckDatab ase)
    {
    GetXmlHttpObjec t() // sets the global variable
    gcItemID = pcID
    gcUrl = "../lib/datacheck.php?c heck=1&tocheck= '" + pcItem + "'"
    goXMLHTTP.onrea dystatechange=s tateChanged
    goXMLHTTP.open( "GET",gcUrl,tru e)
    goXMLHTTP.send( null)

    }
    else
    {
    llIsValid = true
    }
    }
    else
    {
    llIsValid = false
    }
    }
    else
    {
    if (pcItem == null||pcItem == ""||pcItem.leng th < 2)
    {
    llIsValid = false
    }
    else
    {
    llIsValid = true
    }
    }

    if (!plCheckDataba se)
    {
    if (llIsValid)
    {
    document.getEle mentById(pcID). innerHTML = pcDisplay
    if (gnValidationCo unt >= 1)
    {
    gnValidationCou nt = gnValidationCou nt - 1
    }
    }
    else
    {
    document.getEle mentById(pcID). innerHTML = "<span class='warningl abel'>" + pcDisplay + "</span>"
    if (gnValidationCo unt <= pnNumberOfItems )
    {
    gnValidationCou nt = gnValidationCou nt + 1
    }
    }
    }

    if (gnValidationCo unt == 0)
    {
    hideOrShowInput ("complete",fal se)
    }
    else
    {
    hideOrShowInput ("complete",tru e)
    }
    if (plPopulateExtr a)
    {
    populateExtra(l cSource, lcDestination, true, true)
    }
    }
    [/CODE]

    PHP (the page referenced in validateItem())
    [CODE=php]
    <?php

    // establish what is being checked via the $_REQUEST variable
    $lnCheckType = $_REQUEST['check'];
    $lcItemToCheck = $_REQUEST['tocheck'];
    switch($lnCheck Type)
    {
    case 1: // check for presence of username, will only ever be one match
    $lcCheckSQL =
    "SELECT 1 as test
    FROM credential
    WHERE username like '$lcItemToCheck '" ;

    $laResult = $loDB->queryGetData($ lcCheckSQL);

    foreach($laResu lt as $lcTest)
    {
    $llAvailable = $lcTest['test'];
    if($llAvailable > 0)
    {
    echo "<span class='warningl abel'>Username in use:</span>";
    }
    else
    {
    echo "Username:" ;
    }
    }
    break;

    case 2:// just to show that we can expand this system should we ever need to
    echo "case = 2";
    break;
    }
    ?>
    [/CODE]

    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
  • ronnil
    Recognized Expert New Member
    • Jun 2007
    • 134

    #2
    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 :)

    Comment

    • nathj
      Recognized Expert Contributor
      • May 2007
      • 937

      #3
      Originally posted by ronnil
      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:
      [CODE=php]
      -->
      <?php
      // establish what is being checked via the $_REQUEST variable
      $lnCheckType = $_REQUEST['check'];
      $lcItemToCheck = $_REQUEST['tocheck'];

      //establish connection - using dataobject.php did not work?
      $lvCon = mysql_connect(' server', 'user', 'password');
      if (!$lvCon)
      {
      die('Could not connect: ' . mysql_error());
      }
      mysql_select_db ("clfdb", $lvCon);

      // branch the code
      switch($lnCheck Type)
      {
      case 1: // check for presence of username, will only ever be one match or no match
      $lcToDisplay = "Username:" ;

      $lcCheckSQL =
      "SELECT 1 as test
      FROM credential
      WHERE username like $lcItemToCheck" ;

      $result = mysql_query($lc CheckSQL);

      while ($row = mysql_fetch_arr ay($result))
      {
      $lnAvailable = $row['test'];
      if ($lnAvailable = 1)
      {
      $lcToDisplay = "<span class='warningl abel'>Username in use:</span>";
      }
      }

      echo $lcToDisplay;
      break;

      case 2:// just to show that we can expand this system should we ever need to
      echo "case = 2";
      break;
      }
      ?>
      [/CODE]

      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

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        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

        Comment

        • nathj
          Recognized Expert Contributor
          • May 2007
          • 937

          #5
          Originally posted by pbmods
          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

          Comment

          Working...