I am developing a WordPress Widget Plugin that inserts as many as you want email input forms in a widget.
I am using the following code to validate email fields
function validateForm(form) {
var x = document.forms["twb_form"]["email"].value;
var atpos = x.indexOf("@");
var dotpos = x.lastIndexOf(".");
if (x == null || x == "" || atpos< 1 || dotpos<atpos+2 || dotpos+2>=x.length) {
document.getElementById('errorMSG').innerHTML = 'Please enter a valid email address';
return false;
}
}
The problem is that validation error always display along the very first form on page regardless of other forms… This is because of the div #errorMSG
I know this can be solved using some kind of PHP tricks but I can’t figure it out.
Every form on the page is being applied a separate ID … widget-id-1, widget-id-2 and so on…
How can I associate each validation message with a widget-id ?