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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression problem

Status
Not open for further replies.

circlemaker

Programmer
Aug 24, 2004
6
0
0
GB
Hellom

I'm using a regular expression to try and determine if a value entered is valid. The value has to be a number and it has to be exactly 8 numbers in length:

12345678 = valid
11111111 = valid
1234567 = invalid
121212121 = invalid
1234567a = invalid

The code I'm using is below, but it won't work. It determines if the values entered are numeric OK, but if I type in more than 8 numbers it still says its valid

<script language="javascript">
function validate() {
eNo = document.all('name1').value;

if(eNo.match(/[0-9]{8}/)) {
alert("valid");
}
else {
alert("invalid");
}
}
</script>

<form id="Form1" method="post">
<input type="text" name="name1" value=""/>
<input type="button" name="btn" value="Button" onclick="javascript:validate();"/>
</form>

Any help would be appreciated as I stuck on this one.

Thanks

Si
 
You really should avoid using "document.all" if you want your code to work in anything other than IE. I suggest changing this:

Code:
eNo = document.all('name1').value;

to this:

Code:
eNo = document.forms['Form1'].elements['name1'].value;

Hope this helps,
Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
This will work...

Code:
<script language="javascript">
    function validate() {
        eNo = document.getElementById('name1').value;
        eNo_length = document.getElementById('name1').value.length;
     	if((eNo_length < 8) || (eNo_length > 8)) {
	alert("Not Valid"); 
	}
	else {
	alert("valid");
	}
    }
    </script>

<form id="Form1" method="post">
        <input type="text" name="name1" value=""/>
        <input type="button" name="btn" value="Button" onclick="validate();"/>
</form>

But it doesnt matter now because you guys have already answered it!!!. Im getting scared now! Really! Don't you guys have anything better to do?

:)

---------------------------------------
Thanks for your help,
Jon

 
Thanks BabyJeffy, your solution works a treat. I realise that document.all is not good. This page is running on a IE only intranet, so isn't too much of a concern at the moment.

Thanks for the advice though

Si
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top