Regular Expression to allow brackets () to be valiadated - javascript

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.

Related

Regex Comma Separated Phone Number

I am trying to generate a regex which would match the following sequence-
+91123456789,+41123456789,+21123456789.... and so on, there is no limit of phone numbers.
Basically the usage is to validate the phone numbers which users may add, phone number can be multiple and need to be separated by commas, I am already removing the empty spaces which users may add, so no worry for that.
I am not good with regex and have created the following regex but it doesn't matches the preceding phone numbers, means the whole string of phone numbers do not match-
^\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9},\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}$
I need to validate the user input using javascript or jquery.
Valid Phone number should be having country code like +91 or +21 etc country code can be of one or two digits, then the number of digits need to be 7 to 9.
I anyone could help, it would be highly appreciable, I have spent lot of time on this one.
To validate the whole string handling mulitple values sepparated by comma just add an group with * multiplier:
^\+\d{8,11}(,\+\d{8,11})*$
If I understand the requirements correctly, the following regex should work
\+\d{9,11}
However, you can separate the country code out, for if you need to allow for (+44)xxxxxxxxx
\+\d{2}\d{7,9}
if the requirement is to allow for 1 country code as well, adjust the regex to the following
\+\d{1,2}\d{7,10} //I think to 10, not sure on their numbers
You can update the ranges as you see fit :)
Demo: https://regex101.com/r/rJ4wM7/1

Javascript Simple check for webform to make sure it contains both numbers and letters (instead of my code)

I have this code:
if(address.length<=0)
{
msg.setAttribute("style", "color:red");
msg.innerHTML='Please enter address';
return false;
}
I would like to change so it checks whether the webform contains BOTH numbers and letters. Can you help me?
Thank you so much,
Jones
p.s.: So I want to make sure they also enter street name AND house number as well (example: 24 Sunshine street would be good, but if they forget house number, they would get the message).
That doesn't look like PHP at all. More like JavaScript...
Here's one way to do it in JavaScript:
var re = /^\d+\s+\D+$/;
if (re.test(address)) {
//We get here if the address is correctly formated
}
else {
//We get here if the string is badly formated
}
The regex works like this:
\d+ matches to one or more numbers
\s+ matches to one or more spaces
\D+ matches to one or more letters
If you want to accept both "24 Sunshine" and "Sunshine 24" you could instead use this:
/^(\d+\s+\D+)|(\D+\s+\d+)$/
And if we want to be extra safe and protect from the case that the user might enter an extra trailing or leading space we could either trail the string or use this ReGex:
/^\s*(\d+\s+\D+)|(\D+\s+\d+)\s*$/
Apart from regular expression which is a very nice and clear solution you can use these php functions:
first the ctype_alnum () in order to check if your string contains letters and digits and then
this on ctype_alpha() in case the above is true to check if user forgot to enter number.
In case you are interested there is also this one ctype_digit() for checking if user missed the address but gave the number.
Or if you want just a regex this it will do the job:
^[a-zA-Z]([a-zA-Z-]+\s)+\d{1,4}

Regular expression for fax number validation in a JavaScript registration form

I am a complete newbie when it comes to regular expressions. Even after reading guides about them, I still have a hard time formulating my own. I am trying to get my form to validate the fax number in the +12345678910 format. I have some code, but it allows me to submit mixed numbers and letters, as well as submit data with blank spaces and without filling out the faxnum text box completely. I would strongly appreciate any help with the form and tips on how to become more proficient with regex.
var fax = document.registration.faxnum;
faxval(fax);
function faxval(fax)
{
var numbers = /[\+? *[1-9]+]?[0-9 ]+/; //Bad regex
if (fax.value.match(numbers))
{
document.getElementById("faxmsg").innerHTML=("Everything is OK.");
faxmsg.style.color="green";
}
else
{
faxmsg.innerHTML=("Invalid fax number.");
faxmsg.style.color="red";
}
};
To match a number like +12345678910, try: \+1[2-9][0-9]{9}
I assume that the +1 portion is the international number code, 234 is the area code, 567 is the exchange and 8910 is the rest of the number. This pattern would also match a number such as +12125551212.
The first digit of any U.S. or Canadian phone number must be 2 through 9. So we can allow that as the first character ([2-9]). Then the rest of the number will be 9 digits long, 0 through 9 ([0-9]{9}).
If you want to allow - or . too, you can use: \+1(|\.|\-)[2-9][0-9]{2}(|\.|\-)[0-9]{3}(|\.|\-)[0-9]{4}. The (|\.|\-) part means "allow no character OR a . character OR a - character."

Validate numbers, parenthesis and spaces only in jQuery validation

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,''));
}

How to allow only "numbers","-" and "()" using JavaScript

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

Categories