0

Sorry if this question is silly, Please feel free to correct.

I am using the code below for a form validation. How can I use a wildcard so that flagL12, flagM52 and other form ids starting with flag are validated?

var flag = $('#flag').val();
    if(!new RegExp("^[0-9]{2}$").test(flag)){
        $('#require-msg').html("FLAG format invalid! <br>Only 2 digit Numbers allowed for FLAG");
        $('#modal_opener').click();
        e.preventDefault();
        return false;
    }

I tried using ^ after flag but it does not work

    var flag = $('#flag^').val();
    if(!new RegExp("^[0-9]{2}$").test(flag)){
        $('#require-msg').html("FLAG format invalid! <br>Only 2 digit Numbers allowed for FLAG");
        $('#modal_opener').click();
        e.preventDefault();
        return false;
    }
Divyesh Patoriya
  • 518
  • 3
  • 15
Santosh Pillai
  • 1,311
  • 1
  • 20
  • 31
  • What you need wild card? – Satpal Jun 24 '16 at 08:27
  • The attribute startsWith selector is: `$('[id^=flag]')` but your other code doesn't really make sense so you should provide a minimalistic sample replicating your issue. You would probably use: `if($('[id^=flag]').filter(function(){return !new RegExp("^[0-9]{2}$").test(this.value)}).length) {$('#require-msg').html("FLAG format invalid!
    Only 2 digit Numbers allowed for FLAG");}`
    – A. Wolff Jun 24 '16 at 08:27

1 Answers1

0

You can use,

$("[id^='flag']")

This will select all the elements whose id starts with 'flag'

Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53