How to make sure white space after 3 characters using regular expressions - javascript

Actually, I want to validate the Canadian postal code form field using jQuery validation.
so i add the below method to validate Canadian postal code
//Canada zipcode validation methode
jQuery.validator.addMethod("canadazipRegex", function(value, element) {
return this.optional(element) || /^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[ ]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$/i.test(value);
}, "<br>You must enter your postal code in this format: A#A #A#");
using this regular Expression
^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[
]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$
the field should allow only this format
A#A #A#
like :A1A 1A1
Not like:A1A1A1
So,How can i make sure white space after 3 characters.
I am not good with regular Expressions. if you give me good tutorial links also appreciated
Thanks in advance !

Don't make the space optional :
^[ABCEGHJ-NPRSTVXY][0-9][ABCEGHJ-NPRSTV-Z]\s[0-9][ABCEGHJ-NPRSTV-Z][0-9]$
Also there are no needs to use quantifier {1}

\x20 matches will spaces. Try using that

Related

Validate Japanese Email include special characters

I have an Email below:
anzai-kt#itec.hankyu-hanshin.co.jp
Now i want to validate it but not working.
this is my regex:
$scope.emailParten = /^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
Use this regular expression:
/^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/
From: https://www.w3resource.com/javascript/form/email-validation.php
Yours doesn't count "-" as a correct character. Generally validating e-mails is more complex, but this should work for most use cases.
This for example:
very.“(),:;<>[]”.VERY.“very#\\ "very”.unusual#strange.example.com
Is a correct e-mail address, but doesn't get covered by the regular expression in my answer.
If you want to support these weird edge cases try:
/^.+#.+\..+$/
Source: http://codefool.tumblr.com/post/15288874550/list-of-valid-and-invalid-email-addresses

Pattern for form input field validation

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

How to validate algebra expression in javascript

I'm new to Regular expression validations in JavaScript.
My textbox allows the user to enter a formula which needs to be validated client side.
The formula can be made of following operators:
+
_
*
/
(
)
%
These are the only symbols allowed in my textbox.
I need to validate the formula, which should have a form like CurrentbaseSalary+CurrentMBP(BPI). Something like +BPI-CBI( should not be considered valid.
You can use this regexp :
/^(\([+*%\/\-\d\s]+\)|[+*%\/\-\d\s]+)+$/.test('YOUR STRING')
this regexp check if you are using your allowed symbols and if parenthesis matches, but it cant check if "CurrentbaseSalary" is positive or negative, "CurrentMBP(BPI)" neither.
Some code, or more detailed example could help to find a solution.

Regular Expression to allow brackets () to be valiadated

Using Adobe Live cycle, I am creating a form which contains a telephone number field. The telephone number field should only accept numbers, plus symbols and brackets
At the moment I have an expression for validation that accepts pluses and numbers but when I try to add brackets to it, it seems to break it.
if (xfa.event.newText.match(/[^0-9+]/))
{
xfa.event.change = "";
}
Can someone point me into the right direction please. Thanks!!
You want to include the brackets in the character set:
if (xfa.event.newText.match(/[^0-9+()]/)
But note that this doesn't really validate actual phone numbers. This would accept ((())) as a valid phone number.
Validating a phone number is a solved problem, please search around.

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