/    Sign up×
Community /Pin to ProfileBookmark

Validating For Alpha-numeric Fields

Hi everyone, ?

I’ve got the code below so far, and I just cannot figure out what’s wrong with it. I’m trying to use a RegExp (alphaNumeric) to validate the postcode field of my form. I think I’m going the correct way about it, but just not getting there somehow?

Any help on this matter would be greatly appreciated!

Kind regards,

Amph. ?

[code]
function validateFormFields()
{
alphaNumberic = /^[a-zA-Z0-9]+$/;
validateEmail = /^w+([.-]?w+)*@w+([.-]?w+)*.(w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
valid = true;

if (document.frmCreateAcc.firstname.value == “”)
{
alert(“Please enter your first name.”);
valid = false;
document.forms[0].firstname.focus()
return false;
}
if (document.frmCreateAcc.lastname.value == “”)
{
alert(“Please enter your last name.”);
valid = false;
document.forms[0].lastname.focus()
return false;
}
if (validateEmail.test(document.frmCreateAcc.email.value) == false)
{
alert(“Please enter a valid email address”);
valid = false;
document.forms[0].email.focus()
return false;
}
if (alphaNumberic.match(document.frmCreateAcc.postcode.value) == false)
{
alert(“Please enter a valid post code.”);
valid = false;
document.forms[0].postcode.focus()
return false;
}
else
confirm(“Thank you for creating an O2 account.”);

return valid;
}
[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@astupidnameApr 24.2009 — if (alphaNumberic.match(document.frmCreateAcc.postcode.value) == false)[/quote]

The match method does not return true or false, it either returns match/matches or null. So you may want to check for null instead of false, and when checking for null, it is best to use triple equality === , for example, try:

if (alphaNumberic.match(document.frmCreateAcc.postcode.value) === null)

Although, I don't think that's really the best test here, might be better to do something like:


var alphaNumberic = /[^a-zA-Z0-9]/; //disallows anything other than the enclosed character set


<i> </i>if (!alphaNumberic.test(document.frmCreateAcc.postcode.value)) //.test() returns true or false, so if it finds anything other than the above characters...
Copy linkTweet thisAlerts:
@AmphidamasauthorApr 24.2009 — Hmm... It works, kind of. I think I've gone the wrong way about the word boundry... I actually want it to test the field for [B]both[/B] alpha and numeric in the same field. As it stands, if I just type a single letter then it will pass the test and return true. I want it to fail the test unless at least one alpha and one numeric character is entered into the field.

Any ideas what I've done wrong with the word boundry?

Thanks for the help so far! ?
Copy linkTweet thisAlerts:
@AmphidamasauthorApr 25.2009 — Hmm... It works, kind of. I think I've gone the wrong way about the word boundry... I actually want it to test the field for [B]both[/B] alpha and numeric in the same field. As it stands, if I just type a single letter then it will pass the test and return true. I want it to fail the test unless at least one alpha and one numeric character is entered into the field.

Any ideas what I've done wrong with the word boundry?

Thanks for the help so far! ?[/QUOTE]


Bump. ?
Copy linkTweet thisAlerts:
@astupidnameApr 25.2009 — I want it to fail the test unless at least one alpha and one numeric character is entered into the field.

Any ideas what I've done wrong with the word boundry?[/quote]


Not sure exactly what you are asking about "word boundry" as I don't see any regEx word boundary checks ( b ) anywheres. As far as failing the test unless at least one alpha and one numeric, could do something like:
var a = /[^a-zA-Z0-9]/;

var s = document.frmCreateAcc.postcode.value;

if (a.test(s) || !/[a-zA-Z]/.test(s) || !/[0-9]/.test(s)){

If that doesn't fit the bill, perhaps you could clarify the EXACT format/formats of post-code you need to check for.
×

Success!

Help @Amphidamas 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 5.16,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...