javascript - I am having problems getting my form to validate or submit -


my form not seem validating or submitting. submitting , validating before, jquery error messages not displaying @ same time had edit code, , not submitting or validating @ all.

here js:

function validateusername(user) {     var u = document.forms["newuser"]["user"].value     var ulength = u.length;     var illegalchars = /\w/; // allow letters, numbers, , underscores     if (u == null || u == "")     {         return "you left username field emptyyy";     }     else if (ulength <4 || ulength > 11)     {         return "the username must between 4 , 11 characters";      }     else if (illegalchars.test(u))      {         return "the username contains illegal charectors men!";     }     else     {         return "";     } }  function validatepassword(pwd) {     var p = document.forms["newuser"]["pwd"].value     var cp = document.forms["newuser"]["confirmpwd"].value     var plength = p.length;     if (p == null || p == "")     {         return "you left password field empty";     }     else if (plength < 6 || plength > 20)     {         return "your password must between 6 , 20 characters in length";     }     else if (p != cp)     {         return "the passwords not match!"     }     else     {         return "";     } }  function validateemail(email) {     var e = document.forms["newuser"]["email"].value     var elength = e.length;     var emailfilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;     var illegalchars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;      if (elength == "" || elength == null)      {          return "you left email field blank!";     }      else if (e.match(illegalchars))      {          return "ilegal charectors detected exterminate";     }      else      {         return "";     } } function validatefirstname(fname) {     var f = document.forms["newuser"]["fname"].value;     var flength = f.length;     var illegalchars = /\w/;      if(flength > 20)     {         return "first name has max of 20 characters";      }     else if (illegalchars.test(f))     {         return "numbers,letter , underscores in first name only";      }     else      {         return "";     }   }  function validatelastname(lname) {     var l = document.forms["newuser"]["lname"].value;     var llength = l.length;     var illegalchars = /\w/;      if(llength > 100)     {         return  "last name has max of 100 characters";     }     else if (illegalchars.test(f))     {         $("#errorlname").text("numbers,letter , underscores in last name only";     }     else      {         return "";     }   }  function validateform() { /*     valid = true;     //call username function     valid = valid && validateusername();      //call password function     valid = valid && validatepassword();      //call email function     valid = valid && validateemail();      //call first name function     valid = valid && validatefirstname();      //call first name function     valid = valid && validatelastname();      return valid; */       var error = "";     //call username function     error += "\n"+validateusername();      //call password function     error += "\n"+validatepassword();      error += "\n"+validateemail();      error += "\n" + validatefirstname();      error += "\n" + validatelastname();      if(error === ""){         return true;     }     else{          $("#erroruser").text(error);          $("#erroremail").text(error);          $("#errorfname").text(error);          $("#errorpassword1").text(error);          $("#errorlname").text(error);          return false;      }  }    $('#your-form').submit(validateform); 

fiddle: http://jsfiddle.net/cykgd/

you had few errors in code.

  1. the validateform() never being called
  2. line 174: should else if (illegalchars.test(l))
  3. line 113: closing bracket missing

this fiddle seems working now, http://jsfiddle.net/u5565/

make sure jquery included in page. line of code

$(function() {   $('#your-form').submit(function() {      return  validateform();   }); }); 

tells jquery validate form when submitted.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -