Pattern for form input field validation - javascript

I have following scenario, I have to validate the form input field whose first character can be either P or B or R, the second character can only be C and after that 6 digits. I was using this pattern:
pattern="^[PBR]C\d{6,6}$"
Now I have to add one more condition to the input field to allow input of the form BRC100101, so now the input can also start from BR, then letter C and then 6 digits. I tried the below pattern, but couldn't quite get what I'm looking for.
pattern="^(P)(B)(R)(BR)C\d{6,6}$"

Least amount of characters and more professional.
([PBR]|BR)C\d{6}
This way is little easier to read/understand, readability goes a long way.
(P|B|R|BR)C\d{6}
I'm not a regular expresssion expert, I usually use regexr.com to help me build and test the expressions I use in my code.
http://regexr.com/
Demo:
http://regexr.com/3ggi6

You can try this way,
([PBR]|BR)C\d{6,6}
Live Demo: https://regex101.com/r/RN86w1/2

You can try
([PBR]|BR)C\d{6}
Demo https://regex101.com/r/SDgguC/1

Related

HTML Form Validation - Check for Specific Characters

I'm using the Html5 pattern to validate my inputs on forms. I need to make sure an input has the following rules:
A maximum and minimum of 8 characters
The first 3 must be the specific letters wrx (lowercase)
The last 5 must be numbers.
Eg. wrx12345
Can I even do this with pattern or do I need to use JavaScript?
I believe the regex pattern you are looking for is /^wrx[0-9]{5}$/. A visual representation of this here:
And implemented in html:
<input name="example" pattern="^wrx[0-9]{5}$">
You can use regex in Javascript with this regex:
"/[wrz][\d]{5}/g".
To test the minimum = maximum length = 8, you can just test it in javascript.
If the length egual 8, use the regex
Else, show error
I think this could work
You don't need Javascript to do this.
The pattern attribute uses regular expressions so you can use something like this: ^wrx[0-9]{5}$
The ^ and $ indicates the start and end of the string. Then 'wrx' has to be matched exactly and [0-9]{5} looks for 5 number bewteen 0-9.
You can use something like RegExr to test your patterns.

How to get the valid part of a regex match

I want to test if a user string is "ok so far", in that it might not be valid as a whole but it is a subset of a valid one.
I have a regex say ^[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]$
such that "1234-1234-5678-5678" is valid
"1234-12" or even "1" does not match pattern but its a valid subset of a valid format, in other words the input is ok so far.
is there a neat way of doing this without making many many regexes, its friday.
Not sure if I understood well your problem, but I think you want to have something like this:
^([0-9]{4}-){1,3}[0-9]{1,4}$
Working demo
This will match set of 4 digits and can have the last set from 1 to 4 digits
You can also shorten your regex with:
^(\d{4}-){1,3}\d{1,4}$
You could possibly use one final regex for validation of the form you currently have, and a on the fly regex for the user input being valid for each subset.
My idea would be to have ([0-9]{1,4}-)+
For your case this will check as one types:
/^(\d(\d(\d(\d(-(\d(\d(\d(\d(-(\d(\d(\d(\d(-(\d)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?$/
This regex will match key for key as you type, although it is a little cumbersome.
^([0-9]{1,4}|[0-9]{4}-[0-9]{0,4}|[0-9]{4}-[0-9]{4}-[0-9]{0,4}|[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{0,4})$
Here is a live example

Need help for Regex expression

I need a regex expression which will allow following types of input in javascript :-
1,-,2
1,2,-
-,1,2
-
1,2
1
I need it for validation in my website which will allow only above set of values, Please not there could be only one hypen(-) in any case.
1 or 2 in above case can be any number.
Think this is what you need:
^([\d-]+(,[\d-]+)*)?$
Tested via https://regex101.com/

Incorrect Regex Expression

I found this site:
https://mathiasbynens.be/demo/url-regex
and wanted to use for my url validation the regex from the #diegoperini, because according to the table provided on the top of the site, it is the best regex.
When I try to use it, I get a range value error.
P.S. I am using the following Regex expression:
_^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?#)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$_iuS
and the following online validator:
http://regexr.com/
It does show the error place in the regex, but I don't know how to manage it. I tried to swap the both ranges, but it doesn't do the trick.
I would appreciate some help.
P. P. S.
I use the regex in the AngularJS directive to validate url input.
Buried within your character classes, you have this range:
\x{00a1}-\x{ffff}
But it should be:
\u00a1-\uffff
Your expression \x{00a1}-\x{ffff} is not the correct syntax for a hex encoding or a character and as-is means any of "x{}0a1f" plus the range "}-x", but "x" is less than "}" so an error is raised to that effect.
This should work
^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?#)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$

Regex to validate textbox length

I have this RegEx that validates input (in javascript) to make sure user didn't enter more than 1000 characters in a textbox:
^.{0,1000}$
It works ok if you enter text in one line, but once you hit Enter and add new line, it stops matching. How should I change this RegEx to fix that problem?
The problem is that . doesn't match the newline character. I suppose you could use something like this:
^[.\r\n]{0,1000}$
It should work (as long as you're not using m), but do you really need a regular expression here? Why not just use the .length property?
Obligatory jwz quote:
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
Edit: You could use a CustomValidator to check the length instead of using Regex. MSDN has an example available here.
What you wish is this:
/^[\s\S]{0,1000}$/
The reason is that . won't match newlines.
A better way however is to not use regular expressions and just use <text area element>.value.length
If you just want to verify the length of the input wouldn't it be easier to just verify the length of the string?
if (input.length > 1000)
// fail validation

Categories