/    Sign up×
Community /Pin to ProfileBookmark

Form validation – regular expression alert/ submission error

Hi,

I’m new to Javascript so I apologise if this is an obvious query..!

I’m trying to incorporate a RegExp validation into the existing validate function but when a string that matches (I hope) re1 e.g. AB12 12AB is entered I still get the format alert and the form won’t submit.

Any ideas would be very gratefuly received!

[code=php]function validate(node, alerttxt, re) {
with (node)
if (value == null || value == “”) {
alert(alerttxt); return false;
}
else if (re.test(node) ==false) {
alert(alerttxt+ ” format”); return false;
}
}

var re1 = /^D{1,2}d{1,2}s?d{1,2}D{1,2}$/;
var re2 = /^Dw{4}s?w{5}s?w{3}d[!|&|@|?]$/;

function validate_form(thisform) {
with (thisform) {
if (validate(Node1, “Please check Node1 field”, re1) == false)
{ Node1.focus(); return false; }

if (validate(Node2, “Please check Node2 number”,re2) == false)
{ Node2.focus(); return false; }
else {
return true;
}
}
}

<form method=”post” onsubmit=”return validate_form(this)” action=”http://…decode_form.cfm”>[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@MalgrimJul 19.2009 — shouldn't it be re.test(value)
Copy linkTweet thisAlerts:
@WierbleauthorJul 20.2009 — I knew it would be obvious - thank you! ?
×

Success!

Help @Wierble 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.17,
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,
)...