I edited an existing validated function (included in a WordPress plugin) to add email validation feature.
function validateForm(form){
var $ = jQuery;
// The code snippet I added
// **********************************
var emailID = document.getElementsByName('useremail')[0].value;
atpos = emailID.indexOf("@");
dotpos = emailID.lastIndexOf(".");
if (atpos < 1 || ( dotpos - atpos < 2 ))
{
alert("Please enter correct email ID")
return false;
}
// End of the code added by me
else if(!$('input[name=title]', form).val()){
alert("Please enter correct Name")
$('input[name=title]', form).focus();
return false;
}
// ...
return true;
}
The html code:
<input type="text" name="useremail" id="useremail" required>
The email validation code was derived from Validate email address in JavaScript?.
I think the part var emailID = document.getElementsByName('useremail')[0].value;
is not correct.
I tried also to use var emailID = document.getElementById('useremail');
which also does not work.
Added: var emailID = document.getElementById('useremail');
should work. The problem was that the js file was not properly enqueued in WordPress. Thanks for all comments.