Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. <form id='test' onSubmit="return ValidateForm()">
  2. Name * <input id="name" type="text"><span id="wrongname" class="error">This is a required field</span>
  3. Email* <input id="email" type="text"><span id="wrongemail" class="error">This is a required field</span>
  4. <div>
  5. <input type="submit" value="Submit" >
  6. </div>
  7. </form>
  8.  
  9. function ValidateForm() {
  10. document.getElementById('test').onsubmit=function(){
  11. if(document.getElementById('name').value=""){
  12. document.getElementById('wrongname').style.display="block";
  13. return false;
  14. }
  15. else{
  16. return true;
  17. }
  18. if (email.value.search( /^[a-zA-Z]+([_.-]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([.-]?[a-zA-Z0-9]+)*(.[a-zA-Z]{2,4})+$/ ) == -1){
  19. alert(“Wrong email”);
  20. return false;
  21. }
  22. }
  23. }
  24.  
  25. .error{
  26. display:none;
  27. color:red;
  28. }
  29.  
  30. function ValidateForm() {
  31.  
  32. if (document.getElementById('name').value == "") {
  33. document.getElementById('wrongname').style.display = "block";
  34. return false;
  35. }
  36.  
  37. if (document.getElementById('email').value.search(/^[a-zA-Z]+([_.-]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([.-]?[a-zA-Z0-9]+)*(.[a-zA-Z]{2,4})+$/) == -1) {
  38. alert("Wrong email");
  39. return false;
  40. }
  41. }
  42.  
  43. document.getElementById('test').onsubmit = ValidateForm;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement