0

Hello. This is my program:

<html>
<head>
<script type="text/javascript">
function check()
{
if (document.getElementById("age").value!=1)
{
alert("error")
}
}
</script>
</head>
<body>
<form>
Age: <input type="text" id="age" size="30"><br />
Name: <input type="text" id="name" size="30"><br />
<input type="button" value="Check" onclick=check()>
</form>
</body>
</html>

and my question is how to make it display error if I have entered letters in the age box or numbers in a name box? Currently I have only if it is different from 1, so what should I write instead of 1 ?

Edited by XerX: n/a

2
Contributors
4
Replies
6
Views
6 Years
Discussion Span
Last Post by XerX
0
<html>
<head>
<script type="text/javascript">

isFirstName = /^[A-Za-z\ \-]+$/;
isAge       = /^[1-9]\d{0,2}$/;

function check()
{
   if (!isFirstName.test(document.getElementById("name").value))
    {
     alert("invalid age");
    
    }


   if (!isAge.test(document.getElementById("age").value))
    {
     alert("invalid age");
    }
}
</script>
</head>
<body>
<form>
Age: <input type="text" id="age" size="30"><br />
Name: <input type="text" id="name" size="30"><br />
<input type="button" value="Check" onclick=check()>
</form>
</body>
</html>
0

Uhm, thanks, but can lines 5 and 6 be written simpler?

0

It's not working the way it should. Thanks for link. I think I will manage from there. Shouldn't there be "var" in front of lines 5 and 6?

Edited by XerX: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.