below is a my attempt at a function to validate a form field with the prefix ZHA or zha followed by 6 numbers. The prefix part seems to be working but if I enter 1 number it still validates. Any suggestions?
function checkHnum(hnumvalue){
var authTest = /^[ZHA]|[zha]+[\d]{6}$/;
return authTest.test(hnumvalue)
}
Thanks.
Your regex doesn't accept 1 digit only but it's buggy as it, for example, doesn't constraint the order of the letters ([ZHA] is "Z or H or A"). You seem to want
var ok = /^(ZHA|zha)\d{6}$/.test(yourString)
Note that if you also want to accept "Zha123456" then you can simply use a case insensitive regular expression :
var ok = /^zha\d{6}$/i.test(yourString)
Your regex should be:
/^ZHA\d{6}$/i
Note the i to make it case insensitive. The problem with yours was mainly the brackets. The brackets match one of the characters that inside of it.
For example
[ZHA] will match Z, or H, or A, but not the full ZHA
Hope this helps. Cheers
Related
I am trying to take only 2 characters from my phone no.
I have used regex match ^\+55 and this will return the following example.
Phone No : +5546342543
Result : 46342543
Expected Result was only 46.
I don't want to use substring for the answer instead I want to extract that from the phone no with regex.
Can anybody help me on this.
Thank you.
The pattern you used - ^\+55 - matches a literal + in the beginning of the string and two 5s right after.
46 is the substring that appears right after the initial +55. In some languages, you can use a look-behind (see example) to match some text preceded with another.
JavaScript has no look-behind support, so, you need to resort to capturing groups.
You can use string#match or RegExp#exec to obtain that captured text marked with round brackets:
var s = '+5546342543';
if ((m=/^\+55(\d{2})/.exec(s)) !== null) {
document.write(m[1]);
}
This example handles the case when you get no match.
Just try with:
'+5546342543'.match(/^\+55(\d{2})/)[1];
This will get what you want
"+5546342543".match(/^\+55(.*)/)[1]
This solves your problem ?
phoneNumber = "+5546342543"
phone = phoneNumber.substr(3) // returns "46342543"
twoDigits = phoneNumber.substr(3,2) // returns "46"
Using the substr() method as quoted :
The substr() method returns the characters in a string beginning at the specified location through the specified number of characters.
Syntax: str.substr(start[, length])
Source : Mozilla MDN
In my HTML markup, there will be a series of elements with the following naming scheme:
name="[].timeEntries[].Time"
Between both sets of brackets, there will be numbers with at least one possibly two digits. I need to filter out the second set of digits.
Disclaimer: This is my first time getting to know regex.
This is my pattern so far:
var re = /\[\d{1,2}\].timeEntries\[(\d{1,2})\]\.Time/;
I am not sure if I should use the * or + character to indicate two possible digits.
Is replace() the right method for this?
Do I need to escape the period '.' ?
Any other tips you can offer are appreciated.
For example, if I come across an element with
name="[10].timeEntries[9].Time"
I would like to put just the 9 into a variable.
I am not sure if I should use the * or + character to indicate two possible digits.
Neither, use {1,2}
\[\d{1,2}\]\.timeEntries\[(\d{1,2})\]\.Time
Example
This indicates explicitly 1 or 2 digits.
Also, yes, you should escape the .'s
You can use it like this:
var re = /\[\d{1,2}\]\.timeEntries\[(\d{1,2})\]\.Time/;
var myNumber = "[0].timeEntries[47].Time".match(re)[1];
Now myNumber will contain 47.
One final word of warning, myNumber contains the string "47". If your intention is to use it as a number you'll need to either use parseInt or use +:
var myNumber = +"[0].timeEntries[47].Time".match(re)[1];
You're pretty close.
There are a lot of ways you could do this - especially depending on how solid the format of that text will be.
You could use replace:
var re = /\[\d+\]\.timeEntries\[([\d]+)\]\.Time/;
var digits = element_name.replace(re, '$1');
If you know it will always be the second set of digits, you could use match
You could also use indexOf and/or split and some other string functions... In some cases that can be faster (but I think in your case, the regex is fine and probably easier to follow)
I am not sure why this regex expression is not working.
I want to validate if the input is in this format : 12345678,12345678,12345678*space*12345678 , 12345678 , 12345678 , 12345678 12345678,12345678, space
It must be 8 digit if not return false.
Below is the regex expression that i did, But it is working for 2 sets of numbers but when i input another set of numbers validation is not working.
Working: 12345678 , 12345678
Not Working: 12345678 , 12345678 ,12345678
var validate_numbers = /^\s*\d{8}\s*\+*(,\s*\d{8},*)?$/;
Thank you
You need to describe what you want to match in more detail. I'm going to assume you want to match 8-digit nums delimited by commas and pluses, possibly followed by commas.
The problem is you're taking at most 2 sets of digits. Visualization.
Given the assumption above, this is the regex you want:
^(\s*\d{8}\s*[+,]?\s*)*$
Again, you can visualize it on debuggex.
Could you give a little bit more detail about the requirement? Do you need to have a space before comma?
\\d{8}(?:,\\d{8})*+
Try with it. it works fine with requirement that validates a list of numbers, which have 8 digits, and separated by comma.
Hope it will helps
Remove the '$' from your current regular expression. It is strictly matching for the end of the line, which is causing your expression to return false on your desired strings. The following code returns TRUE for the strings that you mentioned which were previously returning FALSE.
omgerd I automatically wrote first response in PHP, here is quick JS edit
var pattern = /^\s*\d{8}\s*\+*(,\s*\d{8},*)?/;
var data2 = '12345678 , 12345678 ,12345678';
if (pattern.test(data2) != 0) {
alert("ok");
}
Output:
ok
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
UK Postcode Regex (Comprehensive)
I have the following code for validating postcodes in javascript:
function valid_postcode(postcode) {
postcode = postcode.replace(/\s/g, "");
var regex = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
return regex.test(postcode);
}
Tests:
CF47 0HW - Passes - Correct
CF47 OHW - Passes - Incorrect
I have done a ton of research but can't seem to find the definitive regex for this common validation requirement. Could someone point me in the right direction please?
Make your regex stricter by adding ^ and $. This should work:
function valid_postcode(postcode) {
postcode = postcode.replace(/\s/g, "");
var regex = /^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/i;
return regex.test(postcode);
}
You want a 'definitive regex' - given all the permutations of the UK postcodes, it needs to be therefore 'unnecessarily large'. Here's one I've used in the past
"(GIR 0AA)|((([ABCDEFGHIJKLMNOPRSTUWYZ][0-9][0-9]?)|(([ABCDEFGHIJKLMNOPRSTUWYZ][ABCDEFGHKLMNOPQRSTUVWXY][0-9][0-9]?)|(([ABCDEFGHIJKLMNOPRSTUWYZ][0-9][ABCDEFGHJKSTUW])|([ABCDEFGHIJKLMNOPRSTUWYZ][ABCDEFGHKLMNOPQRSTUVWXY][0-9][ABEHMNPRVWXY])))) [0-9][ABDEFGHJLNPQRSTUWXYZ]{2})"
Notice I never just use A-Z, for instance, because in each part there are always certain letters excluded.
The problem is the first line of your function. By trimming the spaces out of the target string, you allow false positives.
CF47OHW will match [A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}
CF matches [A-Z]
4 matches [0-9]{1,2}
(blank) matches \s?
7 matches [0-9]
OH matches [A-Z]{2}
W gets discarded
So, as Paulgrav has stated, adding the start and end characters (^ and $) will fix it.
At that point, you can also remove the \s? bit from the middle of your regex.
However! Despite the bug being fixed, your regex is still not going to work how you'd like it to. You should look at the following rather good answer on this here site UK Postcode Regex (Comprehensive)
I have a regex which allows only to enter integers and floats in a text box.
Regex Code:-
("^[0-9]*(?:[.][0-9]*|)$");
But it gives an error when the user enters whitespace at the beginning and end of the entered values. I want the user to allow spaces at the beginning and at the end as optional, so I changed the regex as below but it didn't work.
Note: Spaces may be spaces or tabs.
Test Case: User might enter:
"10","10.23"," 10","10 "," 10.23","10.23 "
Any number of spaces are allowed.
("^(?:\s)*[0-9]*(?:[.][0-9]*|)$")
I am newbie with regex, so any help will be highly appreciated.
Thank you.
Try this:
/^\s*[0-9]*(?:[.][0-9]*|)\s*$/;
You don't have to wrap a single entity in a group to repeat it, and I have added a second zero-or-more-spaces at the end which is what you are missing to make it work.
Note: You have not posted the code you use to create the RegExp object, but if it is new RegExp(string), remember to escape your backslashes (by doubling them):
var r = new RegExp("^\\s*[0-9]*(?:[.][0-9]*|)\\s*$");
Also, as #Blender suggests, this can be simplified to:
/^\s*[0-9]*(?:\.[0-9]*)?\s*$/;
Or, using \d instead of [0-9]:
/^\s*\d*(?:\.\d*)?\s*$/;
You don't necessarily need a Regular Expression: !isNaN(Number(textboxvalue.trim())) would be sufficient.
Otherwise, try /^\s{0,}\d+\.{0,1}\d+\s{0,}$/. Test:
var testvalues = ["10","10.23"," 10","10 "," 10.23","10.23 ","10.24.25"];
for (var i=0;i<testvalues.length;i+=1){
console.log(/^\s{0,}\d+\.{0,1}\d+\s{0,}$/.test(testvalues[i]));
}
//=> 6 x true, 1 x false