How to check uppercase alphabets in input string using jQuery

I am using the following code snippet but it does not work: - (

    //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;
    }
+3
source share
5 answers

you do not need to use substring (). Your regular expression can do all your work. In RegExp, you use matches with characters that are NOT between A and Z. As Avinash said, ^ [AZ] {4} will match if your first 4 characters are capitalized. A "^" at the beginning of your regular expression indicates that the next beginning of the line. When they are placed inside square brackets, it returns the range of characters that you want to match.

+2
source

/[^A-Z]/ /^[A-Z]/

:

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

/[^ A-Z] {4}/ 4 .

0

To detect a change in the middle of large works / ^ [AZ] / to / [AZ] /

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');}
    }
0
source

If they need capital:

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

Or, if they just need to be letters, add ifor the case of ignoring:

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

^means the beginning of the line, and {number}means x copies

0
source

Source: https://habr.com/ru/post/1762265/


All Articles