prevent Duplicate values using Jquery Validation

前端 未结 11 1884
深忆病人
深忆病人 2020-12-04 22:28

I have form and form text field which generates dynamically using JSP. And I\'m using Jquery validation but want to add functionlaty to prevent duplicate entry in the form.<

11条回答
  •  温柔的废话
    2020-12-04 22:43

    Define a function that returns a boolean after making assertion on the validity of the value:

    const isValidGuid = (value) => {
      const validGuid = /^({|()?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}(}|))?$/;
      const emptyGuid = /^({|()?0{8}-(0{4}-){3}0{12}(}|))?$/;
      return validGuid.test(value) && !emptyGuid.test(value);
    };
    

    Use $.validator.addMethod to enable running of the function upon adding the same class name to an element

    $.validator.addMethod("isValidGuid", (value) => isValidGuid(value), "Please select a valid and non empty Guid value.");
    

    Use the class name on your desired element:

     
    

提交回复
热议问题