/    Sign up×
Community /Pin to ProfileBookmark

RegEx Validation Help

[CODE]var adminText = “/^[a-zA-Z][.s][.a-zA-Z]$/”;
var pwdText = “/^(a-zA-Z0-9)$/”;[/CODE]

I’m trying to validate the username and password fields on my login form with regular expressions. The username (adminText) should only contain letters and a whitespace. The password should only contain letters and numbers. Could someone tell me what I’m doing wrong?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@NedalsDec 23.2008 — var adminText = "/^[a-zA-Z][.s][.a-zA-Z]$/";

This will match 'one-alpha + anychar or space + anychar or alpha once

Try..

var adminText = "/^[a-zA-Zs]+$/"; //Note the '+'

Personally I would not allow spaces, and would allow numbers, in the username.

Then it's the same test as password. ?

var pwdText = "/^(a-zA-Z0-9)$/";

This will match one-alphanumeric

Try..

var pwdText = "/^w+$/i";
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorDec 23.2008 — Well, my site is a Role Playing Game, meaning we all play characters. Therefore, that's why I allow letters and spaces only?

Thanks for your help?

Why exactly is your version of pwdText not contained in brackets?

**EDIT: That didn't work. It still keeps coming up as incorrect username. Here's the whole script, in case I'm missing something:

[CODE]// start the function

function adminInput()
{

// set up the regex to make sure the input is correct

var adminText = "/^[a-zA-Zs]+$/";
var pwdText = "/^w+$/i";

if (!document.getElementById) // this script won't work
{

return;

}

else
{

// asign the form fields to variables

var user = document.getElementById("username").value;
var password = document.getElementById("pwd").value;

// if anything is incorrect, alert the user and return false

if (user != adminText)
{

window.alert("Incorrect username.");
return false;

}

else if (password != pwdText)
{

window.alert("Incorrect password.");
return false;

}

else if (user == "" || user == "NULL")
{

window.alert("Please enter a username.");
return false;

}

else if (password == "" || password == "NULL")
{

window.alert("Please enter a password.");
return false;

}

else
{

// let the form submit

return true;

}

}

}
[/CODE]
Copy linkTweet thisAlerts:
@mintedjoDec 23.2008 — Youre doing it wrong :-)

Say you were registering the username "Bob Dole".

This bit of code[CODE]user != adminText[/CODE]checks if "Bob Dole" is the same as "/^[a-zA-Zs]+$/" which you can blatantly see is wrong.

Declare the regex
[CODE]var adminText = /^[a-zA-Zs]+$/;[/CODE]
Then use [CODE]adminText.test(user)[/CODE]
that will return true if uesr matches the pattern, false otherwise.

Obviously you can just invert the result to achieve what you want
[CODE]if(!adminText.test(user)){
//Do stuff
}[/CODE]
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorDec 23.2008 — So JavaScript RegEx doesn't have quotes surrounding it?
Copy linkTweet thisAlerts:
@mintedjoDec 23.2008 — Not when its used in that way. If you use the RegEx constructor then it does.

[CODE]var regex = new RegExp("pattern");[/CODE]
is the same as
[CODE]var regex = /pattern/;[/CODE]
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorDec 23.2008 — All right. Thanks for the help everyone?
×

Success!

Help @Joseph_Witchard spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...