简体   繁体   中英

I need to let the user input any numbers and characters and only the '-' or the '\' in a field. How do I do that with regular expression?

A user can enter a serial number with any numbers or characters and only the following special characters: '-' '\\'. This is what I have in my code:

if(/[-\\a-zA-Z0-9] /i.test(serialNumber)) {
             $('#serialErrorMsg').html('<div>Invalid format.<br>Hover over Serial Number <br>column name to see valid formats</div>');

When I put this into a regex editor and use the following serial; p23edf-A as a test case my alert message comes back as invalid format.

Maybe it's too late (in the evening and I'm tired), but don't you mean to NOT your if expression?

if(! /[-\\a-zA-Z0-9]+/i.test(serialNumber)) {

and to test an entire string change it to

^[-\\a-zA-Z0-9]+$

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