views:

262

answers:

5

i need to validate this type of string

0415256987, 0452 654 987, 0411 236589, 0 410 258 987 etc.

each number has 10 digits and spaces commas, new line, are allowed.

in my project this is a textarea where users enter comma delimited string of mobile numbers. the regex is for the js validation. the spaces and new lines are allowed to give users less frustrating experience of entering the numbers and to accommodate various styles of entering the mobile number

how would you write a regular expression to validate it?

i tried various regex strings and all of them stop working after first comma. i better post my js. perhaps there is some other bug why it does not work. im using jquery validate plugin. i also removed 10digit check as well as it interferes with the validation error msgs and it can be easily fixed (rejected or parsed) on the server.

 $.validator.addMethod("numbersCommasSpacesOnly",   
                       function(value, element){
              return /^(\d\s*\r*\n*-*,*)$/.test(value); 
             }, 
             "Only numbers, spaces and commas are allowed"
              );


    $("#UserContactsOptinForm").validate({
        rules:  {
          "data[User][contacts]": {// compound rule
              required: true, 
              numbersCommasSpacesOnly: true
             }
         },
        messages: {
          "data[User][contacts]":  {
              required: "this field is required"
             }
           }
    });

    });

thank you

+2  A: 

To match a single 10 digit number:

([ \n]*[0-9]){10}

And then to match one or more, separated by commas:

(([ \n]*[0-9]){10}[ \n]*,)*([ \n]*[0-9]){10}

The above regexp at least matches your string, validated using http://www.regextester.com/.

Vincent
You might want to add a `^` and `$` at the beginning and end to make sure there is nothing else before or after in the string.
Brian Campbell
A: 

10 digits with spaces or dashes, followed by a comma or more spaces, and then more of the same:

((\d[\s-]*){10}([\s,]+|\b))+
Kobi
this also seems to be checking only the first number... everything after comma does not validate correctly
ondrobaco
Works well for me: http://jsbin.com/ecavu3 Can you update the question, maybe explain why we get different results? How are you using it?
Kobi
+1  A: 

In Java, you could first do string.replaceAll("\\s*", ""). Then it would match \\d{10}. This will only do matching, not getting different parts e.g. area code, etc.

fastcodejava
+2  A: 
(\d\s*\r*\n*-*,*){10}

should match a 10 digit number with any number of spaces, commas, hyphens and returns.

jkeesh
+3  A: 

First, remove spaces and newlines, then test:

function testNumber(numbers){
    if( !numbers ) return false;
    return numbers.replace(/\s+/gm,'').search(/^(\d{10},)*\d{10}$/)>-1;
}

This works as expected for the test string, and for any form of spacing between digits, and does not allow multiple commas in a row.

To extend this to your code, just replace the function(value,element) code you have with:

function(value,element){
    if( !value ) return false;
    return /^(\d{10},)*\d{10}$/.test(value.replace(/\s+/gm,''));
}

You can replace the {10} with * if want to ignore the digit count.

cmptrgeekken
thank you, this works!
ondrobaco