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) -
<div class="row">
-
<span class="label" id="usernamelabel">
-
<span class="warninglabel">Username:</span>
-
</span>
-
<span class="formw">
-
<input id="username" type="text" size="47" onchange="validateItem('usernamelabel',this.value,'Username:',4,false,5,true);" title="Username, for use on the Forum" />
-
(6-18 alpha numeric characters)
-
</span>
-
</div>
-
Javascript (3 functions involved) -
function stateChanged()
-
{
-
if (goXMLHTTP.readyState==4 || goXMLHTTP.readyState=="complete")
-
{
-
document.getElementById(gcItemID).innerHTML=goXMLHTTP.responseText;
-
}
-
}
-
-
function GetXmlHttpObject()
-
{
-
if (window.XMLHttpRequest)
-
{
-
goXMLHTTP=new XMLHttpRequest()
-
}
-
else if (window.ActiveXObject)
-
{
-
goXMLHTTP=new ActiveXObject("Microsoft.XMLHTTP")
-
}
-
if (goXMLHTTP==null)
-
{
-
alert ("Browser does not support HTTP Request")
-
return
-
}
-
}
-
-
-
function validateItem(pcID,pcItem,pcDisplay,pnType,plPopulateExtra,pnNumberOfItems,plCheckDatabase)
-
{
-
var lcRegExp, llIsValid, llUseRegExp
-
llIsValid = false
-
llUseRegExp = false
-
switch (pnType)
-
{
-
case 1: // UK postcode
-
pcItem = pcItem.toUpperCase()
-
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(lcRegExp))
-
{
-
if(plCheckDatabase)
-
{
-
GetXmlHttpObject() // sets the global variable
-
gcItemID = pcID
-
gcUrl = "../lib/datacheck.php?check=1&tocheck='" + pcItem + "'"
-
goXMLHTTP.onreadystatechange=stateChanged
-
goXMLHTTP.open("GET",gcUrl,true)
-
goXMLHTTP.send(null)
-
-
}
-
else
-
{
-
llIsValid = true
-
}
-
}
-
else
-
{
-
llIsValid = false
-
}
-
}
-
else
-
{
-
if (pcItem == null||pcItem == ""||pcItem.length < 2)
-
{
-
llIsValid = false
-
}
-
else
-
{
-
llIsValid = true
-
}
-
}
-
-
if (!plCheckDatabase)
-
{
-
if (llIsValid)
-
{
-
document.getElementById(pcID).innerHTML = pcDisplay
-
if (gnValidationCount >= 1)
-
{
-
gnValidationCount = gnValidationCount - 1
-
}
-
}
-
else
-
{
-
document.getElementById(pcID).innerHTML = "<span class='warninglabel'>" + pcDisplay + "</span>"
-
if (gnValidationCount <= pnNumberOfItems)
-
{
-
gnValidationCount = gnValidationCount + 1
-
}
-
}
-
}
-
-
if (gnValidationCount == 0)
-
{
-
hideOrShowInput("complete",false)
-
}
-
else
-
{
-
hideOrShowInput("complete",true)
-
}
-
if (plPopulateExtra)
-
{
-
populateExtra(lcSource, lcDestination, true, true)
-
}
-
}
-
PHP (the page referenced in validateItem()) -
<?php
-
-
// establish what is being checked via the $_REQUEST variable
-
$lnCheckType = $_REQUEST['check'];
-
$lcItemToCheck = $_REQUEST['tocheck'];
-
switch($lnCheckType)
-
{
-
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($laResult as $lcTest)
-
{
-
$llAvailable = $lcTest['test'];
-
if($llAvailable > 0)
-
{
-
echo "<span class='warninglabel'>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;
-
}
-
?>
-
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
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 :)
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: -
-->
-
<?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($lnCheckType)
-
{
-
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($lcCheckSQL);
-
-
while ($row = mysql_fetch_array($result))
-
{
-
$lnAvailable = $row['test'];
-
if ($lnAvailable = 1)
-
{
-
$lcToDisplay = "<span class='warninglabel'>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;
-
}
-
?>
-
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
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
|
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.
|
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";
|
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...
| |
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
|
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 :
|
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...
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |