swatisonee Posted November 12, 2006 Share Posted November 12, 2006 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 +3933512345678In 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 ! Link to comment https://forums.phpfreaks.com/topic/27018-disallowing-leading-zeros-having-international-numbers-start-with-a/ Share on other sites More sharing options...
Eugene Posted November 12, 2006 Share Posted November 12, 2006 [code=php:0]if(preg_match('/^[0]{1,}[1-9]{1,}/i', $someareacode)) {return false;}else {return true;}[/code]Something like that. Link to comment https://forums.phpfreaks.com/topic/27018-disallowing-leading-zeros-having-international-numbers-start-with-a/#findComment-123553 Share on other sites More sharing options...
printf Posted November 12, 2006 Share Posted November 12, 2006 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]<?phpif ( 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 Link to comment https://forums.phpfreaks.com/topic/27018-disallowing-leading-zeros-having-international-numbers-start-with-a/#findComment-123554 Share on other sites More sharing options...
swatisonee Posted November 13, 2006 Author Share Posted November 13, 2006 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 Link to comment https://forums.phpfreaks.com/topic/27018-disallowing-leading-zeros-having-international-numbers-start-with-a/#findComment-123737 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.