...

View Full Version : Very Very simple email and phone number validation



brodjah
05-17-2009, 06:15 PM
I am in need of some very simple javascript to validate email address and a phone number from a form.

The email address only needs to:
1. Check that the box is not empty.
2. Check that the text imputted contains an '@' symbol.
3. Check that the text imputted contains atleast 1 Period.

The phone number only needs to:
1. Check that the box is not empty.
2. Check that there is no more than 11 characters.
3. check that only numeric characters have been inputted.

I would like it to report (failure) back as text on the page under the box that has the problem rather than a window.

Thanks,
Mike

bdl
05-17-2009, 07:14 PM
I would suggest posting in the "Web Projects" forum, as you're requesting to have some code done for you as opposed to asking for help on your own code.

jmrker
05-17-2009, 07:20 PM
One time request fulfilled. Still is a better idea to show some attempt.


<html>
<head>
<title>Email - Phone validation</title>
<script type="text/javascript">
// For: http://www.codingforums.com/showthread.php?t=166541

function CheckEmail(info) {
var flag = true;
var obj = document.getElementById('divEmail');
var msg = '';
if (info.indexOf("@") == -1) { msg = "Email missing '@' character"; flag = false; }
if (info.indexOf(".") == -1) { msg = "Email missing '.' character"; flag = false; }
obj.innerHTML = msg;
return flag;
}
function CheckPhone(info) {
var flag = true;
var obj = document.getElementById('divPhone');
obj.innerHTML = '';
if (info.length < 11) { obj.innerHTML = "Phone is less than 11 characters"; flag = false; }
return flag;
}
function Validate() {
flag = true;
if ((CheckEmail(document.getElementById('email').value) == false)
|| (CheckPhone(document.getElementById('phone').value) == false)) { flag = false; }
return flag;
}
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57)) { return false; }
return true;
}

</script>
</head>
<body>
<h2>Email-Phone validation only<h2><p>
Email: <input type="text" id="email" value=""><br>
<div id="divEmail"></div>
Phone: <input type="text" id="phone" value="" onkeypress="return isNumberKey(event)"><br>
<div id="divPhone"></div>

<!-- assume will be incorporated into some sort of <form> at a later time -->
<br><button onClick="if (Validate() == true) { alert('Success'); } else {alert('Failure'); }">Validate</button>
</body>
</html>

Philip M
05-17-2009, 07:44 PM
The email address validation is pretty basic. Anything with an @ and a dot will pass. Even xx@.@.xx@

A much better validation is


if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(eMail.value))) {
alert ("Invalid email address!");
return false;
}

brodjah
05-17-2009, 09:16 PM
Awesome, thankyou guys



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum