Jump to content

Disallowing leading zeros ; having international numbers start with a +


swatisonee

Recommended Posts

Hello,

I'm not able to figure out how to do this.

I have a field for areacode (int) but i dont want users to put in 0345 but only 345.

Similarly, i have another (char)field for international numbers where i want them to put the country code in the number as +3933512345678

In the first case, i'm guessing i need to put in a code like
[code]( if $areacode "starts with a 0") die ("Leading zeros not allowed")  [/code]

but i'm not able to define "starts with a 0" in php.

In the second case if i put the + sign as the default value, for some reason it doesn't load when i load the form. 

Any pointers welcome. Thanks !




You can use a regex or just test the first character or number in the variable. PHP by default can access a single whole variable as an array of parts, so...

[code]<?php

$var = 'hello';

echo $var[4]; // print o

?>[/code]

So if you want to check if a zero is at position (0) in the string you could do...

[code]<?php

if ( isset ( $_POST['areacode'] ) )
{
$test = trim ( $_POST['areacode'] );

if ( $test[0] == '0' )
{
echo 'areacode can not start with (0)';

exit ();
}
}

?>[/code]

There are so many different ways to do this, more information would be needed to really give you the best method to handle your form processing. What I normally do, is create a generic form handler, that uses CASTING, so I set variables to exactly what they should be CAST as, this will save you so much time in validating, which will help you focus more on your real script logic!


Sonia
Thanks a LOT Sonia, G_F_D . I'm going ahead with Sonia's code and i will read about some more on casting. I'm assuming I can use the same code to add the "+" sign as well ???

I think if i use GFD's to verify email addresses, then i should get another problem area solved.

S

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.