Search code examples
twitter-bootstrapjqbootstrapvalidation

how to validate field to contain only specific values?


I have an input field to search for a blood type, I want to validate it so the accepted value should be only one of these strings (A+, A-, B+, B-, O+, O-, AB+, AB-), so when the user typed any other string an error message should appear, I used the identical option, but it can't compare with more than one field, here is my code:

the identical option:

identical:{                                             

    field: 'a+',                                               

    message: 'not a valid blood type'                                      

},

and here is the a+ field:

<input type="hidden" value="A+" name="a+" />

how I can achieve what I want?


Solution

  • You should be able to do this using a regex setting on the validator.

    regexp: {
        regexp: /^(A|B|AB|O)[+-]$/,
        message: 'Invalid blood type'
    },
    

    The regex is taken from this question: Need help with Regular Expression to Match Blood Group