javascript - using REGEX on textInput in react native - Stack Overflow

I can't seem to make my regex work so i think I must be doing something wrong. If anyone could hel

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.

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.

Share Improve this question asked Feb 12, 2020 at 16:25 UnluckyLADUnluckyLAD 1893 silver badges21 bronze badges 1
  • 1 You are not returning any string, you are returning the validation result, true or false – Ian Vasco Commented Feb 12, 2020 at 16:30
Add a ment  | 

1 Answer 1

Reset to default 3

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();
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745632878a4637214.html

相关推荐

  • javascript - using REGEX on textInput in react native - Stack Overflow

    I can't seem to make my regex work so i think I must be doing something wrong. If anyone could hel

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信