开发者

How to check for uppercase alphabets in an input string, using jQuery

I am using following code snippet, but its not working :-(

    //First four characters of input Text should be ALPHABATES (Letters)

    if (($("#txtId").val()).length >= 4) {
        var firstFourChars = $("#txtId").val().substring(0, 4);
        var pattern = new RegExp('[^A开发者_如何学运维-Z]');

        if (firstFourChars.match(pattern))
            isValid = true;
        else
            isValid = false;
    }


change /[^A-Z]/ to /^[A-Z]/

example :

var a = "ABCJabcd";
console.log(a.match(/^[A-Z]{4}/));


you don't need to use substring(). Your regexp can do all the work for you. The RegExp you are using matches against characters that are NOT between A and Z. As Avinash said, ^[A-Z]{4} will match if your first 4 characters are uppercase. "^" at the beginning of your regexp tells that the following should be the beginning of the string. When placed inside square brackets, it reverts the range of characters you want to match.


The regex should be /[^A-Z]{4}/ if you want to match the 4 lowercase characters.


To detect in the middle of the big papers change /^[A-Z]/ to /[A-Z]/

Example text: " asşldla ABCJ abcd AÇALASD"

$('.Order input').change(function (){ucheck($(this).val())});
$('.Order input').keyup(function (){ucheck($(this).val())});

    function ucheck(a) {
        if(a.match(/[A-ZĞÜŞİÖÇ]{4}/)){
$('.Order #Error').html(' UPPERCASE');
}else{$('.Order #Error').html('Capitalize');}
    }


If they need to be capital:

const startsWithCapitals = /^[A-Z]{4}/.test(string);

Or if they just need to be letters, add an i for ignore case:

const startsWithLetters = /^[a-z]{4}/i.test(string);

^ means start of the string and {number} means x copies

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜