Regular expression for format xx-xx (does not contain letters) - javascript

I am trying to use regex to check if the user input is in the correct format xx-xx (input only accepts numbers, does not accept alphanumeric characters)
I tried: /[1-9]{1,}\-[1-9]{1,}/ but when entering alphabetic characters still pass this test.
Can you guys help me. Thank.

Your regex is just fine, you just need to add positional asserts, "^" for the start of the string and "$" for the end of the string:
/^[1-9]{2}\-[1-9]{2}$/
It is better to put "{2}" if you only want xx-xx

/\d{2}-\d{2}/
Worked for me.
Breakdown:
\d checks for a digit character, i.e. 0-9.
{2} checks for two digits next to each other specifically.
- just checks for the hyphen character.

On the other hand, you can have several cases depending on what you are looking for precisely.
In case you agree that an xx-xx value can be 00-00 and any other value, you should use this regex instead:
/^\d{2}-\d{2}$/
In case you accept that an xx-xx value is never 00-00 but can be 01-03 and any other value, but never 00-00, you should use this regex (a bit long, sorry, but does the job perfectly):
/^[1-9](?=[0-9])[0-9]-[1-9](?=[0-9])[0-9]$|^[0-9](?=[1-9])[1-9]-[0-9](?=[1-9])[1-9]$/
Enjoy !

Related

Javascript: How do you check if the string contains either letter/number of both?

I am trying to figure out whether the string contains either letter/number or both.
Right now, I am using
/^[A-Za-z0-9_]+$/gi.test(newFolderName)
That returns true if the text only contains string, ex. "abcd" or only contains numbers, ex. "1234".
That regex doesn't seem to work for the string that contains both string and number. ex. "abcd 1234".
** I would like to allow spaces between letters and numbers.
Could anyone please tell me the regex that can fix this issue?
Thank you.
You can try this regexp:
^[0-9A-Za-z ]+$
Note the whitespace at the end of the sets.
Keep in mind that \w matches all the numbers and all the letters. So you could be more concise, as suggested by ctwheels, by using this syntax:
^[\w ]+$
To require at least 1 letter and 1 number and allow spaces,
use this -
/^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z\d ]+$/

jQuery validation- Input Value combination of number and letter [duplicate]

I have a regex
/^([a-zA-Z0-9]+)$/
this just allows only alphanumerics but also if I insert only number(s) or only character(s) then also it accepts it. I want it to work like the field should accept only alphanumeric values but the value must contain at least both 1 character and 1 number.
Why not first apply the whole test, and then add individual tests for characters and numbers? Anyway, if you want to do it all in one regexp, use positive lookahead:
/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/
This RE will do:
/^(?:[0-9]+[a-z]|[a-z]+[0-9])[a-z0-9]*$/i
Explanation of RE:
Match either of the following:
At least one number, then one letter or
At least one letter, then one number plus
Any remaining numbers and letters
(?:...) creates an unreferenced group
/i is the ignore-case flag, so that a-z == a-zA-Z.
I can see that other responders have given you a complete solution. Problem with regexes is that they can be difficult to maintain/understand.
An easier solution would be to retain your existing regex, then create two new regexes to test for your "at least one alphabetic" and "at least one numeric".
So, test for this :-
/^([a-zA-Z0-9]+)$/
Then this :-
/\d/
Then this :-
/[A-Z]/i
If your string passes all three regexes, you have the answer you need.
The accepted answers is not worked as it is not allow to enter special characters.
Its worked perfect for me.
^(?=.*[0-9])(?=.*[a-zA-Z])(?=\S+$).{6,20}$
one digit must
one character must (lower or upper)
every other things optional
Thank you.
While the accepted answer is correct, I find this regex a lot easier to read:
REGEX = "([A-Za-z]+[0-9]|[0-9]+[A-Za-z])[A-Za-z0-9]*"
This solution accepts at least 1 number and at least 1 character:
[^\w\d]*(([0-9]+.*[A-Za-z]+.*)|[A-Za-z]+.*([0-9]+.*))
And an idea with a negative check.
/^(?!\d*$|[a-z]*$)[a-z\d]+$/i
^(?! at start look ahead if string does not
\d*$ contain only digits | or
[a-z]*$ contain only letters
[a-z\d]+$ matches one or more letters or digits until $ end.
Have a look at this regex101 demo
(the i flag turns on caseless matching: a-z matches a-zA-Z)
Maybe a bit late, but this is my RE:
/^(\w*(\d+[a-zA-Z]|[a-zA-Z]+\d)\w*)+$/
Explanation:
\w* -> 0 or more alphanumeric digits, at the beginning
\d+[a-zA-Z]|[a-zA-Z]+\d -> a digit + a letter OR a letter + a digit
\w* -> 0 or more alphanumeric digits, again
I hope it was understandable
What about simply:
/[0-9][a-zA-Z]|[a-zA-Z][0-9]/
Worked like a charm for me...
Edit following comments:
Well, some shortsighting of my own late at night: apologies for the inconvenience...
The - incomplete - underlying idea was that only one "transition" from a digit to an alpha or from an alpha to a digit was needed somewhere to answer the question.
But next regex should do the job for a string only comprised of alphanumeric characters:
/^[0-9a-zA-Z]*([0-9][a-zA-Z]|[a-zA-Z][0-9])[0-9a-zA-Z]*$/
which in Javascript can be furthermore simplified as:
/^[0-9a-z]*([0-9][a-z]|[a-z][0-9])[0-9a-z]*$/i
In IMHO it's more straigthforward to read and understand than some other answers (no backtraking and the like).
Hope this helps.
If you need the digit to be at the end of any word, this worked for me:
/\b([a-zA-Z]+[0-9]+)\b/g
\b word boundary
[a-zA-Z] any letter
[0-9] any number
"+" unlimited search (show all results)

regular expression for validating password field

I am a learner in java script currently I am going through the regular expression for validating the password field, I want my password field to contain characters and only one number in any place of the string,
I tried the following regular expression it checks for at-least one number and one character in the string
^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$
Can anyone help me!
All you need is adding a lookahead at the start:
^(?=\D*\d\D*$)[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$
The lookahead makes sure there is zero or more nondigits followed by a digit and zero or more nondigits up to the end of string. 
Use this RE
(?!^[0-9]$)(?!^[a-zA-Z]$)^([a-zA-Z0-9]{6,15})$
A simpler expression would be
^\D*\d\D*$
where the meaning is
Zero or more non-numbers
A number
Zero or more non-numbers
If you want at least another character...
^(\D+\d\D*)|(\D*\d\D+)$
where the two provided alternatives require either before or after at least one non-digit.

Cannot construct regex

I am trying to build a regex expression for some validation. I want to check if a string is a combination of atleast one alphabet and one integer. For this i have tried this ^(?=.*[\w][\d]).+ I don't understand regex much. This expression checks for both aplhabet and number in a string but it wants the string to have an alphabet at the start. Instead i just want to check if both alphabet and number are present in a string irrespective of the number and order of occurence. Also the alphabet can be both capital or small so i guess the word checking will be case insensitive. The string might contain special characters along with word and digit in any combination and order but any space should be discarded. Can anyone help?
You'll have to use two lookaheads:
/(?=.*[a-z])(?=.*[0-9])/i
Blender's answer is correct, however I would recommend going for a regex that is easier to understand.
What you're looking for is really one of two scenarios: a string of characters that includes a letter first then a number sometime afterwards or the reverse.
The first scenario would then be: /.*[a-zA-Z].*[0-9].*/.
The second scenario would be: /.*[0-9].*[a-zA-Z].*/.
You can then combine these into one statement:
/(.*[a-zA-Z].*[0-9].*)|(.*[0-9].*[a-zA-Z].*)/
This can be simplified further but I hope this gives you some idea of how to approach regex problems like this.

UK bank sort code javascript regular expression

I'm trying to create a regular expression in javascript for a UK bank sort code so that the user can input 6 digits, or 6 digits with a hyphen between pairs. For example "123456" or "12-34-56". Also not all of the digits can be 0.
So far I've got /(?!0{2}(-?0{2}){2})(\d{2}(-\d{2}){2})|(\d{6})/ and this jsFiddle to test.
This is my first regular expression so I'm not sure I'm doing it right. The test for 6 0-digits should fail and I thought the -? optional hyphen in the lookahead would cause it to treat it the same as 6 0-digits with hyphens, but it isn't.
I'd appreciate some help and any criticism if I'm doing it completely incorrectly!
Just to answer your question, you can validate user input with:
/^(?!(?:0{6}|00-00-00))(?:\d{6}|\d\d-\d\d-\d\d)$/.test(inputString)
It will strictly match only input in the form XX-XX-XX or XXXXXX where X are digits, and will exclude 00-00-00, 000000 along with any other cases (e.g. XX-XXXX or XXXX-XX).
However, in my opinion, as stated in other comments, I think it is still better if you force user to either always enter the hyphen, or none at all. Being extra strict when dealing with anything related to money saves (unknown) troubles later.
Since any of the digits can be zero, but not all at once, you should treat the one case where they are all zero as a single, special case.
You are checking for two digits (\d{2}), then an optional hyphen (-?), then another two digits (\d{2}) and another optional hyphen (-?), before another two digits (\d{2}).
Putting this together gives \d{2}-?\d{2}-?\d{2}, but you can simplify this further:
(\d{2}-?){2}\d{2}
You then use the following pseudocode to match the format but not 000000 or 00-00-00:
if (string.match("/(\d{2}-?){2}\d{2}/") && !string.match("/(00-?){2}00/"))
//then it's a valid code, you could also use (0{2}-?){2}0{2} to check zeros
You may wish to add the string anchors ^ (start) and $ (end) to check the entire string.

Categories