Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FormValidator.prototype.validate = function validateForm() {
- this.errors = {};
- for (var fieldName in this.fields) {
- var field = this.fields[fieldName];
- if (field.hasOwnProperty("required") && field.required && field.elem.value.length == 0) {
- this.addError(fieldName, "This field is required.");
- break;
- }
- if (field.hasOwnProperty("minLength") && field.elem.value.length < field.minLength) {
- this.addError(fieldName, "Input length should not be less than " + field.minLength + " characters.");
- break;
- }
- if (field.hasOwnProperty("maxLength") && field.elem.value.length > field.maxLength) {
- this.addError(fieldName, "Input length should not be greater than" + field.maxLength + " characters.");
- break;
- }
- if (field.hasOwnProperty("ajax")) {
- // FormValidator can't possibly know what the call will return, so we can't add the error here
- // it has to be done manually
- field.ajax(this, field, fieldName);
- }
- }
- if (this.errors.length != 0) {
- // warn the user
- console.log(this.errors);
- }
- };
- var fv = new FormValidator(document.forms[0]);
- fv.addField("login_name", {
- type : "text",
- minLength : 4,
- maxLength : 32,
- required : true,
- ajax : function (fv, field, fieldName) {
- ajax("http://localhost/surec/scripts/user_check.php?field=login_name&value=" + field.elem.value, {
- success : function () {
- var response = JSON.parse(this.response);
- // manually adding the error
- if (!response.error && response.exists) {
- fv.addError(fieldName, "This username is taken.");
- }
- },
- // async: false,
- });
- },
- });
- // called on form submit
- // fv.validate();
Add Comment
Please, Sign In to add comment