简体   繁体   中英

Identify regex is applied to html element using jquery

I want to identify the regex that has been applied to the HTML element using Jquery.

Note: I am working on Asp.net MVC application. HTML elements are already there on CSHTML page, I am just binding events in functions.

Eg I am binding events to the HTML control dynamically as follows.

CreateTextBox("txtUserPassword","/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])([a-zA-Z0-9]{8})$/", "Wrong input");

A function looks like following.

function CreateTextBox(id, RegexText, ValidationMessage)
{
    CreateValidation(id, RegexText, ValidationMessage);
}

Now, when I send data of the textbox to server side using Ajax, first I want to know which regular expression is used for particular control as all controls are being created dynamically.

So, is it possible to set custom property (it should not be visible in the dom like an attribute, also not like data in Jquery) of the text box control?

So we can access jquery property it like following.

var ControlRegex = $("#txtUserPassword").("CustomRegex");

So, variable ControlRegex will be "/^(?=. [0-9])(?=. [az])(?=. [AZ])([a-zA-Z0-9]{8})$/"

Any guesses??

Thanks

You should be able to just compare the regex values:

const a = "/abc/"
const b = "/def/"

function isRegExA(regEx) {
    return regEx === a
}

console.log(isRegExA(a)) //true
console.log(isRegExA(b)) //false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM