JavaScript One Page Validation

-- Leave a Comment
Aello This is a Simple JS Validation example Only to understand how it works.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!doctype html>
<html>
 <head>
  <title>This is a Test JavaScript Validation page</title>
  <script>
   function check() {
    var x = document.forms["test"]["firstname"].value;
    var y = document.forms["test"]["email"].value;
    if (x == "") {
     document.getElementById('error').innerHTML = "Name must be Filled!";
     return false;
    }
    if (y == "") {
     document.getElementById('error').innerHTML = "Email must be Filled!";
     return false;
    }
    var at = y.indexOf("@");
    var dot = y.lastIndexOf(".");
    if (at < 1 || dot < 4 || at > dot ) {
     document.getElementById('error').innerHTML = "Enter a valid email";
     return false;
    }
   }
  </script>
 </head>
 <body>
  <form name="test" method="post" onsubmit = 'return check()'>
   <input type="text" name="firstname" placeholder="Enter a name"/>
   <input type="text" name="email" placeholder="Enter your mail"/>
   <input type="submit" value="Send"/>
  </form>
  <p id="error"></p>
 </body>
</html>

Hey that's not Aello! its Hello..

Hello This is Sagar Devkota. Studying Bachelor of Software Engineering at Pokhara University, NCIT. Open Source Enthusiastic.

0 comments:

Post a Comment