0

I have an input field for phone number and I need it to do something only if the input value contains numbers AND/OR special characters...

I have the following:

var inputphone = $('input#phone').val()

if($.isNumeric(inputphone) || inputphone.match(/.^\+$/)){
    //action
}

But this works only with numbers, if i write the phone number like 0000.000.000 it won't work.

Pat Dobson
  • 3,249
  • 2
  • 19
  • 32
Alin
  • 1,218
  • 5
  • 21
  • 47
  • possible duplicate of [A comprehensive regex for phone number validation](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) – blgt Nov 24 '14 at 16:25
  • @blgt Using regex could work for me but I need it to work internationally too. I only needed a way to permit some characters I define, I don't want to limit the digit number and stuff like that since in my country phone numbers vary from 4 digits to 12 and in various formats. – Alin Nov 24 '14 at 16:33
  • 1
    Both of these concerns have been discussed extensively in the 32 answers to the linked question. This isn't a new problem, so you shouldn't have to reinvent the wheel. If there are *significant* differences that are *not* discussed there, please edit them into the question above – blgt Nov 24 '14 at 16:40
  • Found what I needed ^^ Thanks :) It goes like : /^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$/i – Alin Nov 24 '14 at 16:55

1 Answers1

0

You can replace all the characters used to separe groups of numbers. As explained in this thread jQuery/Javascript find and replace all instances, you can use a regex:

var inputphone = $('input#phone').val()
inputphone.replace(/[\.\-\ ]/g, '');

if($.isNumeric(inputphone) || inputphone.match(/.^\+$/)){
    //action
}

In this case, ., - and [Space] are removed from the inputphone string.

Community
  • 1
  • 1
Alekos Filini
  • 324
  • 2
  • 7
  • can you to explain what does `/.^\+$/` regular? – vp_arth Nov 24 '14 at 16:47
  • well, that is ok but I needed dots and - and a lot more, I actually found this : /^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$/i and it's great – Alin Nov 24 '14 at 16:55