-4

I want a regular expression which accept 3 letters at least and 16 as max and which accept this following : all letters A to Z upper case and lower case and the .(dot) and numbers

I am using JavaScript

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Maroxtn
  • 691
  • 4
  • 7
  • 15
  • What are letters, you first say `3-16` letters, and then you define letters otherwise. – Willem Van Onsem Dec 27 '14 at 13:54
  • In javascript why the votedown ? – Maroxtn Dec 27 '14 at 13:55
  • 1
    @Maroxtn I am not the down voter. I think people are too stupid to understand that this question could be helpful for others (even if it seems duplicated or whatever). – nbro Dec 27 '14 at 13:56
  • @AvinashRaj Do you really give a damn about what he tried? – nbro Dec 27 '14 at 13:57
  • @Barmar What do you want? – nbro Dec 27 '14 at 13:58
  • 1
    @nbro SO is not about getting other people to write code for you. You're supposed to show some effort, post what you tried, explain what problem you're having, and then we'll help you fix it. – Barmar Dec 27 '14 at 13:59
  • 1
    @CommuSoft All letters upper and lower case and and the number of the letters must not reach more than 16 and numbers and . are allowed – Maroxtn Dec 27 '14 at 14:00
  • Your description is not very clear. Can you post examples of strings that should match and strings that shouldn't? – Barmar Dec 27 '14 at 14:00
  • @Barmar I just think that there's not need to down vote this question, even if it's not the clearest question ever posted. – nbro Dec 27 '14 at 14:03
  • @Barmar who told i never tried to make a one and i am not asking from anyone to write code for me i am asking for regex and its allowed and if you want check those question [this](http://stackoverflow.com/questions/5063977/regex-empty-string-or-email) and [this](http://stackoverflow.com/questions/1809613/preg-match-empty-string) and those question are not like my case – Maroxtn Dec 27 '14 at 14:05
  • 2
    If you tried to make it, you should have posted your attempt. Then we could help you understand what you did wrong, and you'll learn. You don't learn from being spoon-fed answers. – Barmar Dec 27 '14 at 14:30
  • The question says that 3 letters are required. The answer you accepted will allow all numbers and dots, with no letters. Did you mean 3 characters? – Barmar Dec 27 '14 at 14:31
  • Yes Stackoverflow: accepted staaaackoverflow : false st4ckoverflow : accepted – Maroxtn Dec 27 '14 at 15:01

1 Answers1

3

A simple regex to do this is the following:

^[A-Za-z0-9.]{3,16}$

The regex works as follows:

  • [A-Za-z0-9.] accepts any character you have specified;
  • {3,16} means repeating it 3 to 16 times; and
  • ^ and $ means the start and end fo the string. So that it does not match other parts of the string.

Thus:

var str = "Wa89dadb...w";
var res = str.match(/^[A-Za-z0-9.]{3,16}$/g); 
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555