jQuery Form Validation, Only Allow .EDU Email Addresses

前端 未结 3 778
无人及你
无人及你 2020-12-19 16:13

I am using the jquery validate function and plugin located here. http://docs.jquery.com/Plugins/Validation

I am going thrue the js file and i have found the email va

3条回答
  •  被撕碎了的回忆
    2020-12-19 16:41

    You can extend it and add your own rule.

    jQuery.validator.addMethod("edu", function(value, element) {
      // Make it work optionally OR
      //  Check the last 4 characters and ensure they match .edu
      return (this.optional(element) || value.slice(-4) == ".edu"); 
    }, "You must use a .edu email address");
    
    $("#myform").validate({
      rules: {
        field: {
          required: true,
          email: true,
          edu: true
        }
      }
    });
    

    Here is a jsfiddle showing how it all works together and how the errors cascade. Just press the button.

提交回复
热议问题