How to write Regular expression for minimum one character in javascript?

14,951

Solution 1

I need minimum of one letter of Alphabets

[a-z]+

and followed by numbers and special characters.

[0-9_\/\s,.-]+

Combined together you would get this:

/^[a-z]+[0-9_\/\s,.-]+$/i

The /i modifier is added for case insensitive matching of alphabetical characters.

Solution 2

Try this regex:

/^[a-z][\d_\s,.]+$/i

To clarify what this does:

^[a-z] // must start with a letter (only one) add '+' for "at least one"
[\d_\s,.]+$ // followed by at least one number, underscore, space, comma or dot.
/i // case-insensitive
Share:
14,951

Related videos on Youtube

Rajasekhar
Author by

Rajasekhar

Updated on June 04, 2022

Comments

  • Rajasekhar
    Rajasekhar almost 2 years

    I have small requirement in Regular expression,here I need minimum of one letter of Alphabets and followed by numbers and special characters. I tried the following regular expressions but I'm not getting the solution.

    /^[a-zA-Z0-9\-\_\/\s,.]+$/
    

    and

    /^([a-zA-Z0-9]+)$/
    
  • Peter Elliott
    Peter Elliott about 11 years
    it might need to be /^[a-z]+[\d_\s,.]+$/, /^[a-z]+[\d_\s,.]*$/ or /^[a-z][\d_\s,.]*$/, depending on if the requirement is "one or more alphabetical characters" followed by either "one or more special characters" or "zero or more special characters"
  • elclanrs
    elclanrs about 11 years
    Yeah it might. I'm guessing a bit here... The question wasn't very clear.
  • Peter Elliott
    Peter Elliott about 11 years
    I agree, and yours is a solid answer, just wanted to provide some possible alternatives in case that didn't exactly fit what the asker was looking for.
  • elclanrs
    elclanrs about 11 years
    I added some clarification in any case.