regular expression

J

Joseph Wu

<html>
<head>
<title>Project 5 Test</title>
<style><!--
body{font-size:18}
input{font-size:24}
--></style>
<script language="JavaScript">
<!--

function SymError()
{
return true;
}

window.onerror = SymError;

var SymRealWinOpen = window.open;

function SymWinOpen(url, name, attributes)
{
return (new Object());
}

window.open = SymWinOpen;

//-->
</script>
<script language="JavaScript"><!--
function do_update() {
var tx = document.myform.mytext.value;
var re = /[0-9]{1,2}/;
if (tx.match(re))
document.bgColor = "lightgreen";
else
document.bgColor = "pink";
}

--></script>
</head>
<body onkeyup="do_update()">
<form name="myform">
<p><input type="text" size="60" name="mytext"> <br>
Test Text </p>
</form>
<script language="JavaScript">
<!--

window.open = SymRealWinOpen;

//-->
</script>
</body>
</html>
 
L

Leif K-Brooks

Joseph said:
i want to make all number with 2 digits valid, but
why it keeps green if i type more than 2 digits?
If you don't anchor a regex to the start and end of text, it will match
if the text contains the pattern. Try changing it to this:
var re = /^[0-9]{1,2}$/;
 
J

Joseph Wu

Joseph said:
i want to make all number with 2 digits valid, but
why it keeps green if i type more than 2 digits?
If you don't anchor a regex to the start and end of text, it will match
if the text contains the pattern. Try changing it to this:
var re = /^[0-9]{1,2}$/;

thank you very much , it works !
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top