Search code examples
jqueryregexmaskmultiline

How to make Regexp Validation in jQuery for multiline text 4x35


I need to make multiline texbox for user to enter text of 4 rows by 35 symbols each row. When I use method that is in TextBox1 it works, but only for A-Za-z, and all other symbols I have to configure in here:

$.mask = {
//Predefined character definitions
definitions: {
'9': "[0-9]",
'a': "[A-Za-z]",
'*': "[A-Za-zА-Яа-я0-9`~!@#$%^&*()_+-=\"]",
'#': "[A-Z0-9]"
}
};

It's not comfortable because I still don't know what symbols in what language will be needed in this textbox.

I think regexp inputmask as in TextBox2 example is good decition, but I'm not strong in JavaScript Regexp to create it (TextBox2 - this Regexp is just for example, I need another one of course).

jQuery(function($){
$("#TextBox1").mask("***********************************
\r\n***********************************
\r\n***********************************
\r\n***********************************");
$('#TextBox2').inputmask('Regex', {
regex: "^[0-9]{2}:[0-5][0-9]:[0-5][0-9]$"
});

Could anybody help to create such regexp - multiline text 4 rows 35 symbols? Thanks for help.


Solution

  • Here is a regex you can use to validate a string having 4 lines and each line having no more than 35 symbols:

    ^(?:.{0,35}\r?\n){3}.{0,35}$
    

    See demo

    The regex breakdown:

    • ^ - Beginning of string
    • (?:.{0,35}\r?\n){3} - 3 sequences of...
      • .{0,35}\r?\n - 0 to 35 characters other than a newline up to optional carriage return and a line feed
    • .{0,35}$ - 0 to 35 characters other than a newline up to the end of string ($).

    Note the use of a limiting quantifier {min,max}, and the use of . to match any character but a newline.