Welcome Guest, Not a member yet? Register   Sign In
Validation for phone no
#1

[eluser]quest13[/eluser]
Can Anyone tell me which validation clause can validate numbers like 123-123-121-1212 ?

When I tried, $this->form_validation->set_rules('Phone','Phone','trim|required|numeric');

It does give error for 123-111-111-1111 as the '-' is not accepted as numeric

When I tried Alpha-numeric, It does accept pure alphabets and doesn't prompt any error message.Same with Alpha-dash.

How to implement a correct validation class for the above format of phone no ?
#2

[eluser]Dam1an[/eluser]
I think your best bet would be to use a custom callback, you can then check against a list of patters so all the following are valid (UK style numbers, not sure of US format)
00000 000000
00000000000
00000-000000
00000 000-000
etc
#3

[eluser]Thorpe Obazee[/eluser]
quest13, try looking into Kohana's phone valid::phone helper. It'll give you an idea.
#4

[eluser]Dam1an[/eluser]
If you're dealing with an application which might expect extension numbers, would this not be a seperate (optional) field?
At least thats the best approach IMO, and lets you put some extra validation in

Edit: Bargainph, you can't say the K word in here, it's like blasphamy Wink
Although it's occasionalyl useful to steal some helpers/librraies, as in most cases, they play nicely together (although helpers in Kohana are static)
#5

[eluser]Thorpe Obazee[/eluser]
[quote author="Dam1an" date="1245422716"]
Edit: Bargainph, you can't say the K word in here, it's like blasphamy Wink
[/quote]
lol.
[quote author="Dam1an" date="1245422716"]
Although it's occasionalyl useful to steal some helpers/librraies[/quote]

Ok, I confess :-)

regardless, K rocks!
#6

[eluser]quest13[/eluser]
Thanks for the information.But my problem is not solved.I do want to display the user that the telephone number is not numeric if he keys in otherwise.But I am not able to use any of the CI library class to fulfill my requirement.

I don't want specific to US or UK phone numbers.My requirement is simple.Firstly, It will check for the blank data, that is easily checked with 'required' class and I did it.Secondly, if the user keys in anything other than numeric or '-' or '+',. I want to prompt the user that the number keyed in is invalid.


Any more suggestions ?


Thanks in advance.
#7

[eluser]Thorpe Obazee[/eluser]
Use regex in a callback function.

Code:
function _check_tel_number($tel_no)
{
    return (bool) preg_match('/\d|\+|\-/', (string) $tel_no);
}
#8

[eluser]naren_nag[/eluser]
I implement this on the browser using a jquery plugin called alphanumeric. It saves me a trip to the server, and since the plugin lets me decide what the user will be allowed to enter, it also helps me avoid a lot of headache trying to validate for every possible type of input.

You could try and combine it with the masking plugin, if you know you are only going to accept a particular kind of telephone number. Though in my experience that hasn't really worked for me.

Links:
itgroup.com.ph/alphanumeric/
digitalbush.com/projects/masked-input-plugin/

cheers




Theme © iAndrew 2016 - Forum software by © MyBB