Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression in Chrome and IE 1

Status
Not open for further replies.

dexeloper

Programmer
Oct 26, 2004
162
GB
These two lines ...

var reInt = /^\d+$/;
if (!reInt(document.getElementById("xx"))){alert("error");}

... work fine in Firefox but in Chrome give me 'object is not a function' for the second line. I don't think anything else is relevant. Just trying to test the input is integer.
Any ideas?
 
Hi

dexeloper said:
work fine in Firefox
Not in my FireFox :
Code:
[blue]>>> reInt = /^\d+$/[/blue]
[green][b]RegExp /^\d+$/[/b][/green]
[blue]>>> reInt(document.getElementById("xx"))[/blue]
[highlight #ffebeb]
[COLOR=white red]?[/color] [red]TypeError: reInt is not a function[/red]
[COLOR=#f4d4d4]?[/color] [green]reInt(document.getElementById("xx"))[/green][right][blue][b]with(_...x")) }; (line 2)[/b][/blue][/right]
[/highlight]
And I would not expect that to work in any browser.
dexeloper said:
Just trying to test the input is integer.
That is easy to answer : never. It will be either an [tt]HTMLElement[/tt] object or [tt]undefined[/tt].
Supposing the element with [tt]id[/tt] xx is an [tt]input[/tt] :
JavaScript:
[b]if[/b] [teal](![/teal]reInt[highlight][teal].[/teal][COLOR=darkgoldenrod]test[/color][/highlight][teal]([/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]"xx"[/i][/green][teal])[/teal][highlight][teal].[/teal]value[/highlight][teal]))[/teal] [teal]{[/teal] [COLOR=darkgoldenrod]alert[/color][teal]([/teal][green][i]"error"[/i][/green][teal]);[/teal] [teal]}[/teal]

Feherke.
 
I can see I made a fopa with the value being checked. However even this gives the same error:

var reInt = /^\d+$/;
if (!reInt("gg")){alert("error");}

Something about regular expressions, objects and functions ...
 
Hi

Check my code again.

You can not pass parameter to an object.


Feherke.
 
Agreed feherke. I wasn't using '.test'.
This works:

var reInt = /^\d+$/;
if (!reInt.test(document.getElementById("xx").value)){alert("error");}

Many thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top