Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation for fax number in javascript using Regex

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.

like image 757
milind Avatar asked Feb 17 '23 07:02

milind


1 Answers

/^\+?[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.

like image 159
Ali Shah Ahmed Avatar answered Mar 04 '23 02:03

Ali Shah Ahmed