Virajsinh

jQuery Validation

Oct 7th, 2021 (edited)
682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 2.89 KB | None | 0 0
  1. // Alphabets with special character (only apostrophe ‘ and space allowed)
  2. $.validator.addMethod(
  3.     "alpa_asp_sp",
  4.     function (value, element) {
  5.         return this.optional(element) || /^[a-zA-Z ']+$/i.test(value);
  6.     },
  7.     "Only apostrophe ‘ and space allowed."
  8. );
  9.  
  10. // Alphanumeric with special character (only back slash / allowed)
  11. $.validator.addMethod(
  12.     "alpnum_bck_sls",
  13.     function (value, element) {
  14.         return this.optional(element) || /^[a-zA-Z0-9/']+$/i.test(value);
  15.    },
  16.    "Only back slash / allowed."
  17. );
  18.  
  19. // Alphanumeric with special character no space Allowed
  20. $.validator.addMethod(
  21.    "alpnum_specil_no_space",
  22.    function (value, element) {
  23.        return this.optional(element) || /^[a-zA-Z0-9/'"._^%$#!~@,-=+*]+$/i.test(value);
  24.    },
  25.    "Space not allowed."
  26. );
  27.  
  28. // alphanumeric and hyphen
  29. $.validator.addMethod(
  30.    "alpnum_dash",
  31.    function (value, element) {
  32.        return this.optional(element) || /^[a-zA-Z0-9-]+$/i.test(value);
  33.    },
  34.    "Alphanumeric with special character (only hyphen allowed)."
  35. );
  36.  
  37. // Alphanumeric with special characters (/ - allowed )
  38. $.validator.addMethod(
  39.    "alpnum_slas_dash",
  40.    function (value, element) {
  41.        return this.optional(element) || /^[a-zA-Z0-9-/]+$/i.test(value);
  42.    },
  43.    "Alphanumeric with special characters (/ - allowed )."
  44. );
  45.  
  46. // date
  47. $.validator.addMethod(
  48.    "greaterThan",
  49.    function (value, element, params) {
  50.        if (!/Invalid|NaN/.test(new Date(value))) {
  51.            return new Date(value) > new Date($(params).val());
  52.        }
  53.        return (isNaN(value) && isNaN($(params).val())) || Number(value) > Number($(params).val());
  54.    },
  55.    "Date Must be greater than {0}."
  56. );
  57.  
  58. //date
  59. $.validator.addMethod(
  60.    "lessThan",
  61.    function (value, element, params) {
  62.        if (!/Invalid|NaN/.test(new Date(value))) {
  63.            return new Date(value) <= new Date($(params).val());
  64.        }
  65.        return (isNaN(value) && isNaN($(params).val())) || Number(value) <= Number($(params).val());
  66.    },
  67.    "Date Must be less than {0}."
  68. );
  69.  
  70. $("#art_registration_form").validate({
  71.    ignore: ":not(:visible)",
  72.    focusCleanup: true,
  73.    onfocusout: function (element) {
  74.        this.element(element);
  75.    },
  76.    rules: {
  77.        patient_name: {
  78.            required: true,
  79.            // maxlength: 50,
  80.            // minlength: 3,
  81.            alpa_asp_sp: true,
  82.            three_min_fifty_max: true,
  83.        },
  84.        date_of_registration: {
  85.            required: true,
  86.        },
  87.    },
  88.    onkeyup: function (element) {
  89.        this.element(element);
  90.    },
  91.    messages: {
  92.        patient_name: {
  93.            required: "Patient Name is Required.",
  94.            asp_sp: "Only apostrophe ‘ and space allowed.",
  95.        },
  96.        date_of_registration: {
  97.            required: "Date of Registration is Required.",
  98.        },
  99.    },
  100. });
  101.  
Add Comment
Please, Sign In to add comment