简体   繁体   中英

using REGEX on textInput in react native

I can't seem to make my regex work so i think I must be doing something wrong. If anyone could help me out that would be great.

here is my regex function

let validatePlate = (plate) => {
  var re = /(^[A-Z]{2}[0-9]{2}\s?[A-Z]{3}$)|(^[A-Z][0-9]{1,3}[A-Z]{3}$)|(^[A-Z]{3}[0-9]{1,3}[A-Z]$)|(^[0-9]{1,4}[A-Z]{1,2}$)|(^[0-9]{1,3}[A-Z]{1,3}$)|(^[A-Z]{1,2}[0-9]{1,4}$)|(^[A-Z]{1,3}[0-9]{1,3}$)|(^[A-Z]{1,3}[0-9]{1,4}$)|(^[0-9]{3}[DX]{1}[0-9]{3}$)/

    return re.test(plate);
};

and I use it in textInput as follows:

<TextInput
onChange={validatePlate}
editable={true}
}}>
Hello
</TextInput>

It still allows special characters, etc.

FYI: The regex is for filtering UK numberplates.

Have you tried wrapping your regexp in new RegExp()

For example:

const nameRegex = new RegExp(/^[a-z ,.'-]+(\s)([a-z ,.'-])+$/i);
if (nameRegex.test(inputValue)) {
    doSomething();
}

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