form name, password, email required js code example
Example 1: how to set validation for email in javascript
<script language="javascript">
function checkEmail() {
var email = document.getElementById('txtEmail');
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email.value)) {
alert('Please provide a valid email address');
email.focus;
return false;
}
}</script>
Example 2: Javascript validate password
let pattern = new RegExp("^(?=(.*[a-zA-Z]){1,})(?=(.*[0-9]){2,}).{8,}$");
let inputToListen = document.getElementById('pass-one');
let valide = document.getElementsByClassName('indicator')[0];
inputToListen.addEventListener('input', function () {
if(pattern.test(inputToListen.value)){
valide.innerHTML = 'ok';
}else{
valide.innerHTML = 'not ok'
}
});