Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
128
rated 0 times [  131] [ 3]  / answers: 1 / hits: 22823  / 11 Years ago, tue, march 5, 2013, 12:00:00

I have one requirement wherein I have to put a validation check on fax number using Regex.The accepted characters in fax number are + . and numbers from 0 to 9. For this I have written the following javascript function



function validateFax(checkField) {
if (checkField.value.length > 0) {
var faxRegEx = /[+? *[1-9]+]?[0-9 ]+/;
if (!checkField.value.match(faxRegEx)) {
return false;
}
}
return true;
}


but it is not helping me to check all acceptable characters. Moreover it checks only 3 to 4 characters, but my fax number can consist of any number of characters. I am new to Regex. Can any one kindly tell me how can I modify this function to make it aligned to my requirement. Thanks in advance.


More From » regex

 Answers
89
/^+?[0-9]+$/


The above regex would allow a + sign at the beginning of the number. Then 0-9 can appear any number of times (more than equals to one). So this regex would allow: +123456789 123456789 etc.


If you want to limit the minimum number of digits, you can modify the regex in the following way:



/^+?[0-9]{6,}$/


here {6,} represents that [0-9] must appear at least 6 times.


[#79833] Monday, March 4, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaelyn

Total Points: 619
Total Questions: 102
Total Answers: 104

Location: Honduras
Member since Sun, Dec 26, 2021
2 Years ago
;