Hi,
I'm trying to do a simple form in HTML, with two Javascript functions, but I can't get it to work. All it's meant to do is have an alert message when the user types in an invalied email or phone number.
Here is the HTML:
Javascript:
So all I've done is added the CheckNumber function to the submit button, but something is not right, it never comes up with the alert. Is the function in the right place in the HTML?
Also, I need to use the CheckEmail function aswell to check the Email, where do I put the function? Can I have them both on the submit button.
If anyone could read over it I would appreciate it. Bear in mind I am a noob at this
Thanks
I'm trying to do a simple form in HTML, with two Javascript functions, but I can't get it to work. All it's meant to do is have an alert message when the user types in an invalied email or phone number.
Here is the HTML:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ClientValidation</title>
</head>
<script type="text/javascript" src="ClientValidation.js">
</script>
<body>
<form action="" method="get">
<label>Name:
<input type="text" name="Name" id="Name" />
</label>
<p>
<label>Surname:
<input type="text" name="Surname" id="Surname" />
</label>
</p>
<p>
<label>Email Address:
<input type="text" name="Email Address" id="Email Address" />
</label>
</p>
<p>
<label>Phone Number:
<input type="text" name="Phone Number" id="Phone Number" />
</label>
</p>
<p>
<label>
<input type="submit" name="button 1" id="button 1" value="Submit" onclick="CheckNumber()" />
</label>
</p>
</form>
</body>
</html>
Javascript:
Code:
<script>
function CheckEmail()
{
var email=document.forms[0].elements[0].value;
var countDot=0;
var countAt=0;
for (var i=0; i<email.length; i++)
{
var c=email.charAt(i);
if (c=='@')
{
countAt++;
}
if (c=='.'
{
countDot++;
}
}
if (countAt>1 || countAt==0 || countDot==0
{
alert ("This is not a valid email address")
return false;
}
else
{
return true;
}
}
function CheckNumber()
{
var PhoneNumber=document.forms[0].elements[0].value;
for (var i=0; i<PhoneNumber.length; i++)
{
var c=PhoneNumber.charAt(i);
if (!(c>0 || c<9))
{
alert("This is not a valid Phone Number");
return false;
}
else
{
return true;
}
}
}
</script>
So all I've done is added the CheckNumber function to the submit button, but something is not right, it never comes up with the alert. Is the function in the right place in the HTML?
Also, I need to use the CheckEmail function aswell to check the Email, where do I put the function? Can I have them both on the submit button.
If anyone could read over it I would appreciate it. Bear in mind I am a noob at this

Thanks