I am able to check for empty field but how do I also check for the valid email checking regular expression? here is what I have so far
<script>
function validateForm()
{
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
var y=document.forms["myForm"]["email"].value;
if (y==null || y=="")
{
alert("email must be filled out");
return false;
}
}
</script>
</head>
<body>
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
First name: <input type="text" name="fname"><br>
email: <input type="text" name="email">
<br><br>
<input type="submit" value="Submit">
</form>
The fist thing you could do is use the HTML5
type="email"attribute., the browser will validate natively. But you must always have a fall-back.Use (found here)
as regualar expression: and do
–
edit after valid comment of PeeHaa
You must not forget to also validate server side, if you are using PHP you can use preg_match with the same regexp