简体   繁体   中英

How to use regex expression for letters numbers and alphanumeric characters

Can anyone help me with my regex expression. Im trying to validate the date string as follows 27 August 1979 - 05:25 am can anyone help me to fix my code by using uppercase lowercase hyphen, colon text and numbers.

Code

regula.custom({
    name:'AlphaSpecial',
    defaultMessage: "The text field can only contain letters,numbers and alphanumeric characters!",
    validator:function(){
        return /^[a-zA-Z0-9_.:-]*$/.test(this.value)
    }
})

You could try matching ^\\w+\\s\\d{4}\\s-\\s\\d{2}:\\d{2}\\s\\w{2}$

To learn more about regex I would recommend using https://developer.mozilla.org/nl/docs/Web/JavaScript/Guide/Regular_Expressions

And to test your regex this site is great: https://regex101.com/

I suggest for dates (with months in english)

^([012]?\d|3[01])\s[JFMASOND][a-z]{2,}\s(\d{2}|\d{4})\s-\s([01]\d|2[0-3]):[0-5]\d$

([012]?\\d|3[01]) = digit(s) for the day 01 or 1 to 31

\\s = space

[JFMASOND] = Begin the month with uppercase.

[az]{2,} = At least 2 more characters in lowercase (this part can be improved).

\\s = space.

(\\d{2}|\\d{4}) = Year with 2 or 4 digits.

\\s-\\s = space + hyphen + space

([01]\\d|2[0-3]):[0-5]\\d = hour (00:00 to 23:59)

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