Re: regex test failing in form validation



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
.



Relevant Pages

  • Re: Fastest way to search a string for the occurance of a word??
    ... but the OP's question was what's the "Fastest way to search a string ... in all the tests I did here, the Regex was by far superior. ... However, of course, if you've got new regular expressions all ... Sure - but just that extra Match object could be relevant if the search ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Search for multiple things in a string
    ... >>> As far as readability, it has nothing to do with Regular Expressions ... > and Regex. ... >> characters in the string, perhaps even writing your own state machine ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: String.replaceAll wont work :s
    ... the String#replaceAllmethod expects a regular expression (regex) ... function (just like in java Strings). ... The second parameter is no regex, but only the String which ... For more information about regular expressions, ...
    (comp.lang.java.help)
  • Re: Regular Experssion
    ... that regular expressions are so powerful, how come Cor was able to whip up a ... didn't whip up a regex example to show how much faster and cooler regex is? ... <flame off> ... especially if search and match string are long. ...
    (microsoft.public.dotnet.general)
  • Re: Regular Experssion
    ... that regular expressions are so powerful, how come Cor was able to whip up a ... didn't whip up a regex example to show how much faster and cooler regex is? ... <flame off> ... especially if search and match string are long. ...
    (microsoft.public.vstudio.general)