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 ?

<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>

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

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.