I have to do phone number validation using JavaScript.
I have already done validation for numbers as follows,
var filter =/^[0-9]+$/
But now I have to also allow hyphen and "()".
Please provide me a way for the same.
How about:
/^[0-9()-]+$/
Notes:
Parenthesis have no special meaning in a character set
If you start, or end, with a minus-sign, it is not interpreted as a range, but as the minus-character itself
Related
I have to set some rules on not accepting wrong url for my project. I am using regex for this.
My Url is "http ://some/resource/location".
This url should not allow space in beginning or middle or in end.
For example these spaces are invalid:
"https ://some/(space here in middle) resource/location"
"https ://some/resource/location (space in end)"
"(space in starting) https ://some/resource/location"
"https ://(space here) some/resource/location"
Also these scenario's are invalid.
"httpshttp ://some/resource/location"
"https ://some/resource/location,https ://some/resource/location"
Currently I am using a regex
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*#)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%#!\-\/]))?/;
This regex accepts all those invalid scenarios. I am unable to find the correct matching regex which will accept only if the url is valid. Can anyone help me out on this?
We need to validate n number of scenarios for URL validation. If your particular about your given pattern then above regex expression from other answer looks good.
Or
If you want to take care of all the URL validation scenarios please refer In search of the perfect URL validation regex
/(ftp|http|https){1}:\/\/(?:.(?! ))+$/
is this regex OK ?
use this
^\?([\w-]+(=[\w-]*)?(&[\w-]+(=[\w-]*)?)*)?$
See live demo
This considers each "pair" as a key followed by an optional value (which maybe blank), and has a first pair, followed by an optional & then another pair,and the whole expression (except for the leading?) is optional. Doing it this way prevents matching ?&abc=def
Also note that hyphen doesn't need escaping when last in the character class, allowing a slight simplification.
You seem to want to allow hyphens anywhere in keys or values. If keys need to be hyphen free:
^\?(\w+(=[\w-]*)?(&\w+(=[\w-]*)?)*)?$
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.
See this fiddle: http://jsfiddle.net/5vTc7/
If you open the console, you can see that the regular expression in the pattern attribute ((?=^[0-9]*(\.[0-9]+)?$)(?=.*[1-9])) works as expected from JS, but when you enter anything in the input and try to submit, it fails.
In case there's something wrong with my regular expression, I'm simply trying to limit it to numbers greater than 0. I'd like to use the number input (i.e., <input type="number"/>), but I can't, because it doesn't allow you to format the values (e.g., it will display 0.00000001 as 1e-8, which is undesirable).
I am clueless here. Is there something I'm missing? Why doesn't this work?
When you use the pattern with anchors, as specified in The pattern attribute, it will fail with Javascript as well
var pattern = '^(?=^[0-9]*(\.[0-9]+)?$)(?=.*[1-9])$';
var reg = new RegExp(pattern);
console.log(reg.test('1.0')); // will fail
console.log(reg.test('0.0')); // will fail
See modified JSFiddle
If you want to limit the input to non-null numbers, you can use
\d*[1-9]\d*(?:\.\d*)?|\d+\.\d*[1-9]\d*
This pattern requires at least one non-null digit either before or after the decimal point.
See JSFiddle
You can try this pattern:
^(?:0+\.0*[1-9][0-9]*|0*[1-9][0-9]*(?:\.[0-9]+)?)$
I am trying and failing hard in validating a phone number within jQuery validation. All I want is to allow a number like (01660) 888999. Looking around the net I find a million examples but nothing seems to work. Here is my current effort
$.validator.addMethod("phonenumber", function(value) {
var re = new RegExp("/[\d\s()+-]/g");
return re.test(value);
//return value.match("/[\d\s]*$");
}, "Please enter a valid phone number");
Bergi is correct that the way you are constructing the regular expression is wrong.
Another problem is that you are missing anchors and a +:
var re = /^[\d\s()+-]+$/;
Note though that a regular expression based solution will still allow some inputs that aren't valid phone numbers. You can improve your regular expression in many ways, for example you might want to require that there are at least x digits, for example.
There are many rules for what phone numbers are valid and invalid. It is unlikely you could encode all those rules into a regular expression in a maintainable way, so you could try one of these approaches:
Find a library that is able to validate phone numbers (but possibly not regular expression based).
If you need a regular expression, aim for something that is a close approximation to the rules, but doesn't attempt to handle all the special cases. I would suggest trying to write an expression that accepts all valid phone numbers, but doesn't necessarily reject all invalid phone numbers.
You may also want to consider writing test cases for your solution. The tests will also double as a form of documentation of which inputs you wish to accept and reject.
You need to use either a regex literal or a string literal in the RegExp constructor:
var re = /[\d\s()+-]/g;
// or
var re = new RegExp("[\\d\\s()+-]", "g");
See also Creating a Regular Expression.
Apart from that, you would need to use start- and end-of-string anchors to make sure that the regex matches the whole string, not only a part of it, and some repetition modifier to allow more than one character:
var re = /^[\d\s()+-]+$/g;
Another approach may be:
function(value) {
return /^\d+$/.test(value.replace(/[()\s+-]/g,''));
}
and if you want to check for the length of the number too, say it has to be a string with 10 digits:
function(value) {
return /^\d{10}$/.test(value.replace(/[()\s+-]/g,''));
}
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