Beginner RexEx question

J

Jessica.Sexton

So now I have to create a regular expression using javascript, this is
what I have so far:


<script>

function numbersOnly(e){

var redigits=/(^-?\d\d*$)/; //regular expression defining numbers

if (document.form1.firstInput.value.search(redigits)==-1) //if match
failed

if (document.form1.secondInput.value.search(redigits)==-1)

return false //disable key press

}

</script>


-----------------------------------------------------------------------------------------------------------------
And then for the input I have:

<input onkeypress="return numbersonly(event)" type="text"
name="firstInput">





However it doesn't work, so what am I doing wrong? Also is there a
more efficient way to do the if statements without having to list
"firstInput.value" and then "secondInput.value" on the next
line?



Thanking you in advance,
Jessica
 
E

Evertjan.

wrote on 24 mei 2006 in comp.lang.javascript:

function numbersOnly(e){

where do you use the "e" ?
var redigits=/(^-?\d\d*$)/; //regular expression defining numbers

the () are superfluous
if (document.form1.firstInput.value.search(redigits)==-1) //if match
failed

Use test(), since you are testing on a complets string.
Search is for finding the position in a string.
if (document.form1.secondInput.value.search(redigits)==-1)

return false //disable key press

}

</script>

------------- And then for the input I have:

<input onkeypress="return numbersonly(event)" type="text"
name="firstInput">

should be numbersOnly(), not numbersonly()

There is no "event" here necessary, and you do not use it

You are in trouble here, because since the added character is not yet
added, as you suspected, you cannot test for it.

============================

The whole idea of testing during the typing is bound to annoy your users
anyway, so better test onsubmit.

============================

Try this:

<form onsubmit='return numbersOnly(this)' name='form1'>
<input type="text" name="firstInput">
<input type="text" name="secondInput">
<input type=submit>
</form>

<script type='text/javascript'>
function numbersOnly(d){
result = /^-?\d+$/.test(d.firstInput.value) &&
/^-?\d+$/.test(d.secondInput.value)
if (!result) alert('Both must be whole numbers!')
return result
}
</script>
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Wed, 24 May 2006 09:45:50 remote, seen in
news:comp.lang.javascript, (e-mail address removed) posted :
function numbersOnly(e){

var redigits=/(^-?\d\d*$)/; //regular expression defining numbers

var redigits=/(^-?\d+$)/; //regular expression defining numbers

if (document.form1.firstInput.value.search(redigits)==-1) //if match
failed

if (document.form1.secondInput.value.search(redigits)==-1)

return false //disable key press

var DF1 = document.form1
if (!redigits.test(DF1.firstInput.value) &&
!redigits.test(DF1.secondInput.value))
return false //disable key press


However it doesn't work, so what am I doing wrong? Also is there a
more efficient way to do the if statements without having to list
"firstInput.value" and then "secondInput.value" on the next
line?

function UG(X) { return !redigits.test(X.value) }

if (UG(DF1.firstInput) && UG(DF1.secondInput)) return false


Your original routine would have returned, AFAICS, undefined if it
completed; and undefined would tend to be taken as false.

function OK(X) { return redigits.test(X.value) }

function numbersOnly(e) {
var DF1 = document.form1
if (OK(DF1.firstInput.value)) return true
if (OK(DF1.secondInput.value)) return true
return false }

ALL UNTESTED. See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,222
Latest member
patricajohnson51

Latest Threads

Top