I have been trying to validate my form email input with these line of codes.
function validate() { var emailFirst = document.getElementById('email1'); var emailFirstRGex = /^(([^<>()[\\]\\\\.,;:\\s@\\"]+(\\.[^<>()[\\]\\\\.,;:\\s@\\"]+)*)|(\\".+\\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$/; var emailFirstTest = emailFirstRGex.test(emailFirst); if (emailFirstTest === false) { alert('hello20'); } }
My form
<form class="needs-validation" novalidate method="POST" action="nomination2.php" onsubmit="validate()">
I keep getting the hello20 response. Meanwhile i have tried this regex against emails on the console without error. How do I solve this?
emailFirstTest
is declared inside your function, so checking for it outside the function means it is out of scope and doesn't exist.
Just define it above your function:
var emailFirstTest = '';
function validate() {
emailFirst = document.getElementById('email1');
var emailFirstRGex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
var emailFirstTest = emailFirstRGex.test(emailFirst);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.