verify input value in javascript 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: form validation for email in js
<script>
function validateform(){
var name=document.myform.name.value;
var password=document.myform.password.value;
if (name==null || name==""){
alert("Name can't be blank");
return false;
}else if(password.length<6){
alert("Password must be at least 6 characters long.");
return false;
}
}
Example 3: html form validation
<form action="">
<label for="name">Username</label>
<input id="name" type="text" required />
<label for="Email">Email</label>
<!-- The required attribute will prompt the user to make sure -->
<!-- the fields are not empty, and that the email is formatted correctly -->
<!-- if you want to use custom validation you should use regex in JavaScript -->
<input id="Email" type="text" required />
</form>