Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript validation 2

Status
Not open for further replies.

GoldPearl

Programmer
Aug 9, 2005
64
hello ppl,
can anybody tells me wat is wrong with the following code because it is skipping the last condition ("Your user ID is of the wrong format....)

function uname()

{
var iChars = "!@#$%^&*()+=-[]\\\';,/{}|\":<>?";
var iChars1 = "/^\w+\.\w+$/";

if (frmusermaint.txtuserid.value.length == 0)
{
alert('Please enter your user ID');
frmusermaint.txtuserid.focus();
return false;
}
else
{
for (var i = 0; i < frmusermaint.txtuserid.value.length; i++)
{
if (iChars.indexOf(frmusermaint.txtuserid.value.charAt(i)) != -1)
{
alert ("Your user ID has special characters.\n These are not allowed.\n Please remove them and try again.\n ");
frmusermaint.txtuserid.select();
return false;
}
}
}

if (((frmusermaint.txtuserid.value.length < 6) || (frmusermaint.txtuserid.value.length > 25)) && (frmusermaint.txtuserid.value.length)!= 0)
{
alert("The user ID must be between 6-25 characters long.\n");
frmusermaint.txtuserid.select();
return false;
}

else
{
for (var j = 0; j < frmusermaint.txtuserid.value.length; j++)
{
if (iChars1.indexOf(frmusermaint.txtuserid.value.charAt(j)) != -1)
{
alert ("Your user ID is of the wrong format.\n Required format is like 'jane.keen'. Please try again.\n ");
frmusermaint.txtuserid.select();
return false;
}

}
}


}


Cheers,
Gold Pearl
 
You are using a regular expression incorrectly in the last test. The following will do the same thing:
Code:
...
var myRegularExpression = new RegExp(/^([a-z])+\.([a-z])+$/gi)
if (!myRegularExpression.test(frmusermaint.txtuserid.value)) alert("Use the correct format (xxxx.yyyy)");
...
Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
ok modifications made but the form doesn't submit even when everything is ok.

code:
....

function checkWholeForm()
{
return textonly() && textonly1() && uname();

}
....
//function to check userid
function uname()

{
var iChars = "!@#$%^&*()+=-[]\\\';,/{}|\":<>?";
var iChars1 = "/^\w+\.\w+$/";

if (frmusermaint.txtuserid.value.length == 0)
{
alert('Please enter your user ID');
frmusermaint.txtuserid.focus();
return false;
}
else
{
for (var i = 0; i < frmusermaint.txtuserid.value.length; i++)
{
if (iChars.indexOf(frmusermaint.txtuserid.value.charAt(i)) != -1)
{
alert ("Your user ID has special characters.\n These are not allowed.\n Please remove them and try again.\n ");
frmusermaint.txtuserid.select();
return false;
}
}
}

if (((frmusermaint.txtuserid.value.length < 6) || (frmusermaint.txtuserid.value.length > 25)) && (frmusermaint.txtuserid.value.length)!= 0)
{
alert("The user ID must be between 6-25 characters long.\n");
frmusermaint.txtuserid.select();
return false;
}

else
{

var myRegularExpression = new RegExp(/^([a-z])+\.([a-z])+$/gi)
if (!myRegularExpression.test(frmusermaint.txtuserid.value))
alert("Your user ID is of the wrong format.\n Required format is like 'jane.keen'. Please try again.\n ");
return false;
frmusermaint.txtuserid.select();
}

return true;
}
....
....
<input name="btnadd" type="submit" id="btnadd" value="Add" onclick="return checkWholeForm()">


can anybody tells me wat's wrong?


 
Ahh... minor logic error easily fixed by adding some brackets:
Code:
...
        var myRegularExpression = new RegExp(/^([a-z])+\.([a-z])+$/gi)
        if (!myRegularExpression.test(frmusermaint.txtuserid.value))
[COLOR=red][b]{[/b][/color]
        alert("Your user ID is of the wrong format.\n Required format is like 'jane.keen'. Please try again.\n ");
        return false;
        frmusermaint.txtuserid.select();
[COLOR=red][b]}[/b][/color]
...
Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Replace this last else { } part by something like this.
[tt]
else
{
if (frmusermaint.txtuserid.value.match(/^\w+\.\w+$/) {
alert("Your user ID is of the wrong format.\n Required format is like 'jane.keen'. Please try again.\n ");
frmusermaint.txtuserid.select();
return false;
}
}
[/tt]
ps. Sorry, GoldPearl, for mixing up on the regexp in the last thread, I think this /^\w+\.\w+$/ is the correct version as kaht took time and kind enough to correct my mistake.
 
Amendment: the corresponding line should be read like this.
[tt]
if (frmusermaint.txtuserid.value.match(/^\w+\.\w+$/)[red])[/red] {[/tt]

 
Amendment-2: the corresponding line should really be read like this.
[tt]
if ([red]![/red]frmusermaint.txtuserid.value.match(/^\w+\.\w+$/)[red])[/red] {
[/tt]
 
thks BabyJeffy & tsuji...the braces slipped out of my attention to.

it works perfect now.

Gold Pearl.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top