Hi I'm trying to have a user enter their email and have it added to the element in the array while the window is open. I did that below, but I wanted the form to focus if the user enters an email wrong. So if they enter chris/@yahoo.com, it would highlight the exact "/" and say "error please reenter." The same goes for other characters that don't belong. It does this now but I want it to focus on the part that is wrong. Also if the email is shorter than 5 characters I'd want it to focus on the whole 5 characters and state another error. How would I do this? Also instead of having just the check button, Is there a way to have a "Find" button as well to find emails instead of having one button do both? Any help is appreciated.
Thank You
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add Email Address</title>
<script type="text/javascript">
var emails = [];
function checkEmail(inputvalue){
var pattern=/^([a-zA-Z0-9_.-])[email protected]([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
if(pattern.test(inputvalue)){
return true;
}
return false;
}
function addEmail(form) {
var statusBox=document.getElementById('status');
var emailBox = document.getElementById('email_address');
emailBox.style.borderColor = '';
statusBox.style.display='block';
var emailOk = checkEmail(form.email.value);
var found = false;
for(emailIndex in emails) {
if(emails[emailIndex]==form.email.value) {
found = true;
break;
}
}
if(emailOk && !found) {
emails.push(form.email.value);
statusBox.innerHTML = 'Email added.';
statusBox.style.color = 'lime';
}
else {
statusBox.innerHTML = found ? 'Email alread added' : 'Email invalid.';
statusBox.style.color = 'red';
emailBox.style.borderColor = 'red';
emailBox.focus();
}
}
function resetStyle(elId) {
var el=document.getElementById(elId);
el.style.backgroundColor = '';
el.style.borderColor = '';
el.style.color = '';
}
function lookUpEmails(inputBox) {
var suggestBox = document.getElementById('suggest');
var found = false;
suggestBox.innerHTML = '';
for(var emailIndex in emails) {
if(new RegExp('^'+inputBox.value,'i').test(emails[emailIndex])) {
suggestBox.innerHTML += "<div onclick=\"selectEmail('"+emails[emailIndex]+"');\">"+emails[emailIndex]+"</div>";
found = true;
}
}
suggestBox.style.position = 'absolute';
suggestBox.style.top = (inputBox.offsetTop+inputBox.offsetHeight+3)+'px';
suggestBox.style.left = inputBox.offsetLeft+'px';
suggestBox.style.width = inputBox.style.clientWidth+'px';
suggestBox.style.display = found ? 'block' : 'none';
}
function selectEmail(selectedAddress) {
document.getElementById('email_address').value = selectedAddress;
hideBox('suggest');
}
function hideBox(boxId) {
document.getElementById(boxId).style.display = 'none';
}
function init() {
document.onclick=function () {
hideBox('suggest');
}
var emailBox = document.body.getElementById('email_address');
emailBox.onclick = function (e) {
var event = e || event;
e.cancelBubble = true;
return false;
}
}
</script>
</head>
<body onload="init()">
<div id="status"> </div>
<form name="signupform" action="#" onsubmit="addEmail(this);return false;">
Input your email: <input name="email" type="text" class="inputs" id="email_address"
value="[email protected]" size="35" maxlength="255" onkeyup="lookUpEmails(this);""><br />
<input name="submit" type="submit" value="Check">
<div id="suggest" style="display: none;border: 1px solid black;"> </div>
</form></body></html>
Thank You
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add Email Address</title>
<script type="text/javascript">
var emails = [];
function checkEmail(inputvalue){
var pattern=/^([a-zA-Z0-9_.-])[email protected]([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
if(pattern.test(inputvalue)){
return true;
}
return false;
}
function addEmail(form) {
var statusBox=document.getElementById('status');
var emailBox = document.getElementById('email_address');
emailBox.style.borderColor = '';
statusBox.style.display='block';
var emailOk = checkEmail(form.email.value);
var found = false;
for(emailIndex in emails) {
if(emails[emailIndex]==form.email.value) {
found = true;
break;
}
}
if(emailOk && !found) {
emails.push(form.email.value);
statusBox.innerHTML = 'Email added.';
statusBox.style.color = 'lime';
}
else {
statusBox.innerHTML = found ? 'Email alread added' : 'Email invalid.';
statusBox.style.color = 'red';
emailBox.style.borderColor = 'red';
emailBox.focus();
}
}
function resetStyle(elId) {
var el=document.getElementById(elId);
el.style.backgroundColor = '';
el.style.borderColor = '';
el.style.color = '';
}
function lookUpEmails(inputBox) {
var suggestBox = document.getElementById('suggest');
var found = false;
suggestBox.innerHTML = '';
for(var emailIndex in emails) {
if(new RegExp('^'+inputBox.value,'i').test(emails[emailIndex])) {
suggestBox.innerHTML += "<div onclick=\"selectEmail('"+emails[emailIndex]+"');\">"+emails[emailIndex]+"</div>";
found = true;
}
}
suggestBox.style.position = 'absolute';
suggestBox.style.top = (inputBox.offsetTop+inputBox.offsetHeight+3)+'px';
suggestBox.style.left = inputBox.offsetLeft+'px';
suggestBox.style.width = inputBox.style.clientWidth+'px';
suggestBox.style.display = found ? 'block' : 'none';
}
function selectEmail(selectedAddress) {
document.getElementById('email_address').value = selectedAddress;
hideBox('suggest');
}
function hideBox(boxId) {
document.getElementById(boxId).style.display = 'none';
}
function init() {
document.onclick=function () {
hideBox('suggest');
}
var emailBox = document.body.getElementById('email_address');
emailBox.onclick = function (e) {
var event = e || event;
e.cancelBubble = true;
return false;
}
}
</script>
</head>
<body onload="init()">
<div id="status"> </div>
<form name="signupform" action="#" onsubmit="addEmail(this);return false;">
Input your email: <input name="email" type="text" class="inputs" id="email_address"
value="[email protected]" size="35" maxlength="255" onkeyup="lookUpEmails(this);""><br />
<input name="submit" type="submit" value="Check">
<div id="suggest" style="display: none;border: 1px solid black;"> </div>
</form></body></html>
Comment