Re: Regular Expression for validating a url field



Tizzah wrote:

What is wrong with that?

regex =
/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]
{1,5})?\/.*)?$/

if(field.hpage.value != regex.test(field.hpage.value)){
alert("Bad Homepage")
field.hpage.focus()
field.hpage.select()
return false
}
return true
}

(Do not use the tab character for indentation, at least in postings.
Use multiples of two or four space characters instead.)

Plenty of things are wrong with this. From top to bottom, and left to
right:

- `(https?)' is equivalent to `(http|https)' and more efficient than
the latter.
- Valid domain names may contain uppercase ASCII characters.
- Valid domain names may contain more than one consecutive hyphen (`-'),
ref. IDN, and they may begin or end with a hyphen or a dot.
- The literal hyphen does not need to be escaped at the beginning
or the end of a character class (`[...]').
- The literal dot (`.') does not need not to be escaped in a character
class.
- The {1} quantifier is redundant always.
- ([0-9]{1,5})? is equivalent to \d{,5} (not considering backreferences).
- Valid domain names may contain more than 5 consecutive decimal digits.
- Valid top-level domain names must not contain any decimal digit.
- Valid top-level domain names are not restricted to five letters, and
the .test TLD specified in RFC2606 for testing purposes has only four
letters.
- A URI does not need to include the path delimiter `/' if there are no
further path components.

The regex should be all right...

For appropriate values of "all right".

also the name for the fields are 100% okay.. if I change the != for ==,
it will go throught... which is completely wrong =o(

Your code simply does not make sense. RegExp.prototype.test() returns a
boolean value, either `true' or `false'. You are comparing that value
against a supposed string value, and since you do not do perform a strict
comparison (`!==' or `==='), you are forcing implicit type conversion on
both operands. (Skip the following section if you are not interested in
the inner workings of the language.)

,-[ECMAScript 3 Final]
|
| 11.9.2 The Does-not-equals Operator ( != )
|
| The production
| EqualityExpression : EqualityExpression != RelationalExpression
| is evaluated as follows:
|
| 1. Evaluate EqualityExpression.
| 2. Call GetValue(Result(1)).
| 3. Evaluate RelationalExpression.
| 4. Call GetValue(Result(3)).
| 5. Perform the comparison Result(4) == Result(2). (Section 11.9.3.)
| [...]
| 11.9.3 The Abstract Equality Comparison Algorithm
|
| The comparison x == y, where x and y are values, produces true or false.
| Such a comparison is performed as follows:
|
| 1. If Type(x) is different from Type(y), go to step 14.

Type(x) = String, Type(y) = Boolean. Condition applies, go to step 14.

| [...]
| 14. If x is null and y is undefined, return true.
| 15. If x is undefined and y is null, return true.
| 16. If Type(x) is Number and Type(y) is String,
| return the result of the comparison x == ToNumber(y).
| 17. If Type(x) is String and Type(y) is Number,
| return the result of the comparison ToNumber(x) == y.
| 18. If Type(x) is Boolean, return the result of the comparison
| ToNumber(x) == y.

None of the above applies, continue.

| 19. If Type(y) is Boolean, return the result of the comparison
| x == ToNumber(y).

Condition applies. Return the result of x == ToNumber(y).

x_1 := x
y_1 := y

| The comparison x == y, where x and y are values, produces true or
| false.

x := x_1
y := ToNumber(y_1).

| 1. If Type(x) is different from Type(y), go to step 14.

Case 1: y_1 = false (no match). ToNumber(false) = 0 --> y := 0.
Case 2: y_1 = true (match). ToNumber(true) = 1 --> y := 1.

In both cases:

Type(x) = String, Type(y) = Number. Condition applies, go to step 14.

| 14. If x is null and y is undefined, return true.
| 15. If x is undefined and y is null, return true.
| 16. If Type(x) is Number and Type(y) is String,
| return the result of the comparison x == ToNumber(y).

None of the above applies, continue.

| 17. If Type(x) is String and Type(y) is Number,
| return the result of the comparison ToNumber(x) == y.

x_2 := x
y_2 := y

| The comparison x == y, where x and y are values, produces true or
| false.

x := ToNumber(x_2)
y := y_2.

| 1. If Type(x) is different from Type(y), go to step 14.

Case 1: x_2 = "" (empty string). ToNumber("") = 0 --> x := 0.

Case 2: x_2 = "N" (not empty). In that case, ToNumber("N") always
returns a number value. If "N" is not the string representation
of a numeric literal, that value is NaN.

In both cases:

Type(x) = Number, Type(y) = Number. Condition does not apply,
continue.

| 2. If Type(x) is Undefined, return true.
| 3. If Type(x) is Null, return true.
| 4. If Type(x) is not Number, go to step 11.
| 5. If x is NaN, return false.

This condition applies if x_1 is not a string representation
of a numeric literal, read: could be a URI. In that case,
`false' is returned to the calling algorithm, so ultimately
`false' is returned to the algorithm of `!=', its Result(5)
being `false':

| 5. Perform the comparison Result(4) == Result(2). (Section 11.9.3.)

Result(5) := false

| 6. If Result(5) is true, return false. Otherwise, return true.

Therefore, `true' is returned then!
___________

If the condition ("x is NaN") does not apply, i.e. x_1 can be
interpreted as a number (read: is definitely not a URI), continue.

| 6. If y is NaN, return false.

This applies never here, continue always.

| 7. If x is the same number value as y, return true.

Case 1: x = y. Applies if

- x_1 (being the value of field.hpage.value) is the empty string,
because "" is converted to 0, and there can be no match for
/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5
(([0-9]{1,5})?\/.*)?$/ in "", so `false' [being the result of
regex.test(field.hpage.value)] is converted to 0. x = y = 0.

- x_1 is "0...0" or "0x0...0", and there is no match, because x_1
is converted to 0, and `false' is converted to 1. x = y = 0.

- x_1 is "0...01" and "0x0...1", and there is a match, because
x_1 is converted to a 1, and `true' is converted to 1. x = y = 1.
Since the Regular Expression never matches "0...01" or "0x0...1",
this sub-case never applies.

`true' is returned here to the calling algorithm, and to its calling
algorithm, so ultimately `true' is returned to the algorithm of
`!=', its Result(5) being `true':

| 5. Perform the comparison Result(4) == Result(2). (Section 11.9.3.)

Result(5) := true

| 6. If Result(5) is true, return false. Otherwise, return true.

Therefore, `false' is returned then!
___________

Case 2: x != y. Applies if

- x_1 is different from "", "0...0" and "0x0...0", and there is
no match, because x_1 is then converted to a value n != 0, and
`false' is converted to 0. 0 != n = x != y = 0.

The condition

| 7. If x is the same number value as y, return true.

would not apply in case 2, therefore we continue.

| 8. If x is +0 and y is -0, return true.
| 9. If x is -0 and y is +0, return true.

None of above applies, continue.

| 10. Return false.

`false' is returned here to the calling algorithm, and to its calling
algorithm, so ultimately `false' is returned to the algorithm of `!=',
its Result(5) being `false':

| 5. Perform the comparison Result(4) == Result(2). (Section 11.9.3.)

Result(5) := false

| 6. If Result(5) is true, return false. Otherwise, return true.

Therefore, it returns `true' then!
_______________________________________________________________________

The outcome of the algorithm for the `==' operator is (of course) the
boolean opposite of the algorithm result for the `!=' operator, and
vice-versa.

So if the control's value is "", the (equals-)condition

field.hpage.value == regex.test(field.hpage.value)

is true:

0. "" == false ("no match")
1. "" == ToNumber(false)
2. "" == 0
3. ToNumber("") == 0
4. 0 == 0
5. true

If the control's value is "0...0" or "0x0...0", the condition is true:

0. "0...0" == false ("no match")
1. "0...0" == ToNumber(false)
2. "0...0" == 0
3. ToNumber("0...0") == 0
4. 0 == 0
5. true

If the control's value is "0...1" or "0x0...1", or another value that
can be interpreted as a number different from 0, the condition is false:

0. "0...1" == false ("no match")
1. "0...1" == ToNumber(false)
2. "0...1" == 0
3. ToNumber("0...1") == 0
4. 1 == 0
5. false

If the control's value is "http://f/"; (not a URL, according to your
standards), the condition is false.

0 "http://f/"; == false ("no match")
1. "http://f/"; == ToNumber(false)
2. "http://f/"; == 0
3. ToNumber("http://f/";) == 0
4. NaN == 0
5. false (according to 11.9.3, step 5)

If the control's value is "http://x.org"; (a URL, according to your
standards), the condition is _false_:

0 "http://x.org"; == true ("match")
1. "http://x.org"; == ToNumber(true)
2. "http://x.org"; == 1
3. ToNumber("http://x.org";) == 1
4. NaN == 1
5. false


You are looking for

if (!regex.test(field.hpage.value))
{
alert("Bad Homepage");
// ...
return false;
}
return true;

and probably a Regular Expression for matching URLs that makes sense,
see RFC3986.


PointedEars
.



Relevant Pages

  • Re: Microsoft.VisualBasic.Strings.Asc ?
    ... I'm trying to put together a simple utility to do rc4 encryption based on the rc4 algorithm here: ... (I know .net has builtin support for many crypto algorithms but the original vbscript version of this RC4 'like' algorithm is already in use for something which this utiltiy I want to make will be used for. ... You don't need to use Substring to access a single character in a string, and you don't need ToCharArray to convert a single character string to a character. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problem with TEA implementation - help needed
    ... The charCodeAtmethod returns NaN if you ask ... for a character index past the end of the string. ... could it be that your Strings stop at the first NUL ...
    (sci.crypt)
  • Finding multiple occurrences of character in word document
    ... multiple occurrences' of a specific character in one sentence. ... I know the basic idea behind the algorithm and I know how to implement ... into a string. ... search for another comma. ...
    (microsoft.public.word.vba.general)
  • Re: Hash- Defined
    ... Any set therefor denotes algorithm as functional relation. ... A string relation as a subset exists. ... s equals character string or symbol set. ...
    (sci.crypt)
  • Hash- Defined
    ... Any set therefor denotes algorithm as functional relation. ... A string relation as a subset exists. ... s equals character string or symbol set. ... Sequnece location one causes the function. ...
    (sci.crypt)

Loading