/    Sign up×
Community /Pin to ProfileBookmark

Using regex for validation

Hi guys. I’m pretty new to JS, and in particular regular expressions. I have a form which needs validating. The particular input I am having trouble with has to be formatted as 2 capital letters followed by any amount of numbers, e.g. AB123456. No other symbols are allowed, and the order is as described. Does any one have a solution they could share?
Thanks in advance.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceApr 15.2005 — Two capital letters plus one or more digits:
if(!/^[A-Z]{2}d+$/.test(fld.value)) {
alert("Invalid format.");
return false;
}
return true;

[URL=http://msdn.microsoft.com/library/en-us/script56/html/js56jsgrpregexpsyntax.asp][i]Regular Expression Syntax[/i][/URL]
Copy linkTweet thisAlerts:
@d_moauthorApr 15.2005 — Thanks for that, but it didn't work. I copied it exactly, only changing the 'fld' to the input box name, keyed in the correct format into the input box, but the validation failed.
Copy linkTweet thisAlerts:
@Warren86Apr 15.2005 — ..
Copy linkTweet thisAlerts:
@phpnoviceApr 15.2005 — Thanks for that, but it didn't work. I copied it exactly, only changing the 'fld' to the input box name, keyed in the correct format into the input box, but the validation failed.[/QUOTE]
Sorry, the way I call my functions, [b]fld.value[/b] would have worked. For you, you would need to change [b]fld.value[/b] to:

document.formName.fieldName.value

I copied that code out of a working form -- I was already checking for the same format. (Imagine that!)

I also use the [b]onchange[/b] event for my validation for two reasons. #1 No [b]focus()[/b] is required. #2 I don't know about now, but even just a year ago there was a "problem" with the order in which the [b]onblur[/b] event and the [b]onfocus[/b] event were fired. In other words, the [b]onfocus[/b] event for the next field would fire before the [b]onblur[/b] event fired for the previous field. Thus, if you were using the [b]onblur[/b] event for validation on two successive fields, your browser would go into a validation loop while the two fields fought over the focus.
Copy linkTweet thisAlerts:
@d_moauthorApr 16.2005 — Thanks to all. I changed the field value to document.formName.fieldName.value, and magic, it worked a treat.
Copy linkTweet thisAlerts:
@phpnoviceApr 16.2005 — Cheers.
×

Success!

Help @d_mo 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.19,
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,
)...