Guest User

Untitled

a guest
Jun 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. FormValidator.prototype.validate = function validateForm() {
  2. this.errors = {};
  3. for (var fieldName in this.fields) {
  4. var field = this.fields[fieldName];
  5.  
  6. if (field.hasOwnProperty("required") && field.required && field.elem.value.length == 0) {
  7. this.addError(fieldName, "This field is required.");
  8. break;
  9. }
  10.  
  11. if (field.hasOwnProperty("minLength") && field.elem.value.length < field.minLength) {
  12. this.addError(fieldName, "Input length should not be less than " + field.minLength + " characters.");
  13. break;
  14. }
  15.  
  16. if (field.hasOwnProperty("maxLength") && field.elem.value.length > field.maxLength) {
  17. this.addError(fieldName, "Input length should not be greater than" + field.maxLength + " characters.");
  18. break;
  19. }
  20.  
  21. if (field.hasOwnProperty("ajax")) {
  22. // FormValidator can't possibly know what the call will return, so we can't add the error here
  23. // it has to be done manually
  24. field.ajax(this, field, fieldName);
  25. }
  26. }
  27. if (this.errors.length != 0) {
  28. // warn the user
  29. console.log(this.errors);
  30. }
  31. };
  32.  
  33. var fv = new FormValidator(document.forms[0]);
  34.  
  35. fv.addField("login_name", {
  36. type : "text",
  37. minLength : 4,
  38. maxLength : 32,
  39. required : true,
  40. ajax : function (fv, field, fieldName) {
  41. ajax("http://localhost/surec/scripts/user_check.php?field=login_name&value=" + field.elem.value, {
  42. success : function () {
  43. var response = JSON.parse(this.response);
  44. // manually adding the error
  45. if (!response.error && response.exists) {
  46. fv.addError(fieldName, "This username is taken.");
  47. }
  48. },
  49. // async: false,
  50. });
  51. },
  52. });
  53.  
  54. // called on form submit
  55. // fv.validate();
Add Comment
Please, Sign In to add comment