Re: regex test failing in form validation
- From: Conrad Lender <crlender@xxxxxxxxx>
- Date: Mon, 01 Jun 2009 00:02:42 +0200
On 31/05/09 22:27, lancemiller777 wrote:
1) http://lance-miller.appspot.com/?page=webform
2) see the webform.js link at top of that page.
3) turn on debug ( an onclick box upper right )
4) enter URL's into HOMEPAGE field. e.g. http://example.com
5) If you have debug on you'll see a decent report of what the code is
doing.
Problem: The regex for matching correct protocol and TLD for the URL
is apparently doing the wrong thing, and I've been stuck for days.
I don't want to appear overly negative, but there's a _lot_ wrong with
your code... I recommend to read up on regular expressions, and use
common coding conventions (see below). I didn't have time to look at the
whole thing, but here are a few starting points:
var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/g
- Use semicolons to end your statements (yes, really).
- When you test for validity, use a whitelist instead of a blacklist.
- You don't need to escape *every* character in a regex. The same
thing could be done with /[()<>,;:\\"\[\]]/
var alphaFilter = /\W/g
This will actually exclude all alphanumeric characters: a-z, A-Z, and
0-9 (and the underscore). Did you mean \w?
var digitFinder= /\d/g
var underscoreFinder= /_/g
var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/g
This will also match " @ . __".
But to be honest, accurately validating e-mail addresses (per RFC822, I
think, or the updated version) is HARD.
var protocol
var TLDlist = "(com)|(edu)|(net)|(org)|(uk)|(us)|(gov)|(mil)|(info)|(biz)"
// YOU edit this TLDlist to change to desired list e.g. "com|edu|org"
is regex saying "com OR edu OR org"
var TLDregex= /^(TLDlist)$/g
This will search for the string "TLDlist". You can't interpolate
variables in JavaScript, neither in strings nor regular expressions. I
think that may be the reason for your difficulties. Look at the RegExp
constructor if you want to build your expressions dynamically (google
for "mdc regexp").
It's also unnecessary to put each of the listed domains into a separate
group; /(com|edu|org|whatever)/ is enough to capture a group.
I don't have time to look at the rest of your script, but a few other
issues should definitely be addressed; for example, what do you expect
this to do?
userErrorMsg=="" ? userErrorOut.style.display=myDisplay[0] :
userErrorOut.style.display=myDisplay[1]
HTH.
- Conrad
.
- Follow-Ups:
- Re: regex test failing in form validation
- From: Dr J R Stockton
- Re: regex test failing in form validation
- From: Conrad Lender
- Re: regex test failing in form validation
- References:
- regex test failing in form validation
- From: lancemiller777
- regex test failing in form validation
- Prev by Date: Re: javascript parameter isn't passing
- Next by Date: Re: regex test failing in form validation
- Previous by thread: regex test failing in form validation
- Next by thread: Re: regex test failing in form validation
- Index(es):
Relevant Pages
|