Please could some one help me out with a bit of javascript and a simple contact form.
I found this code on youtube and it's really cool however at the moment i can only get it to work with the 'email address' validation.
What it does is, if you enter a correct email format a green tick image displays and if you enter an incorrect email address format it displays an X for incorrect.
Also if the field is blank, by default it displays the X image.
What I would like to do is the same sort of thing but for a few more fields in my contact form. Obviously these fields have different requirements, than having to enter a valid email format.
For example in the 'phone number' field I would like it to only accept numbers and maybe () and + symbols.
//Just in case someone enters a country code number format +44 (0) 207 for example.
Anyway I hope this all make sense, I tried to do this on my own but I cant seem to get it working right as I am very new to javascript.
Many thanks to anyone who can help me out,
Cheers.
<html>
<head>
<script type="text/javascript">
var status = document.getElementById("status");
var email = document.getElementById("email");
function checkEmail(email)
{
var reg1 = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if(reg1.test(email) == false) {
document.getElementById("status").src = "/images/bad.png";
} else if(reg1.test(email) == true) {
document.getElementById("status").src = "/images/ok.png";
}
}
</script>
</head>
<body>
<form action="process_form.php" method="post">
<table width="500" cellspacing="5" cellpadding="0">
<tr>
<td>Email Address:</td><!--Has to be a valid email address format to display the 'ok' image -->
<td><input type="text" id="email" name="email" onkeyup="checkEmail(this.value);" onblur="checkEmail(this.value);" maxlength="60" size="30"/></td>
<td><img src="images/bad.png" alt="Status" id="status" style="margin-left:5px; vertical-align:middle;" /></td>
<td> </td>
</tr>
<tr>
<td width="136">Account Number:</td><!--Has to be more than 4 characters long -->
<td width="144"> </td>
<td width="48"> </td>
<td width="145"> </td>
</tr>
<tr>
<td>Name:</td><!--No numbers and has to be more than 4 characters long -->
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Phone Number:</td><!--Should only allow numbers and the following characters: + () -->
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>












