Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Validation - Check word in String

With Jquery Validation for Bootstrap plugin, I have a form.

Now that I want to check a field that must contain a specific word. Like

Field have the word 'COMPLUSORY_WORD' in the string - return valid

Field don't have the word 'COMPLUSORY_WORD' in the string - return invalid

What additional method should I include in validation script?

like image 944
user3045457 Avatar asked Nov 18 '25 16:11

user3045457


1 Answers

You can try like this:

if (yourstring.indexOf("COMPLUSORY_WORD") >= 0)

You can also make your search case insensitive like this:

if (yourstring.toLowerCase().indexOf("COMPLUSORY_WORD") >= 0)

EDIT:

You can try to use it like this":

function checkWord(yourstring, compulsaryword)
{
  return new RegExp( '\\b' + compulsaryword + '\\b', 'i').test(yourstring);
}

EDIT:

You can try this:

jQuery.validator.addMethod('COMPLUSORYWORDCHECK', function (value, element) { 
    return this.optional(element) || /^\bCOMPLUSORY_WORD\b$/.test(value);
}, "Compulsary word");
like image 118
Rahul Tripathi Avatar answered Nov 21 '25 09:11

Rahul Tripathi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!