This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 2

Changing phone number validation


  1. philwareham
    Member

    Hi there,
    I wish to use the 'phone' field in a form on a UK-centric website, where there are numerous ways people can enter their phone number (for instance some numbers are ##### ###### whilst others are #### ### ##### etc). Having set the field option to 'international' I'm still getting an error message on submit to enter a valid phone number.
    Is there any way of manually adjusting the code to accommodate this? I would use the simple 'numbers' field but that does not seem to like the use of spaces.
    Thanks,
    Phil

    Posted 16 years ago #
  2. yuniar

    The 'international' phone format accept +XXXXXXX or XXXXXX numbers.
    The minimum length is 3 numbers and spaces are not allowed.

    If you would like to allow spaces, edit your includes/common-validator.php file.
    Search around line 145 for this code:

    function validate_simple_phone($value){
    
    	$error_message = VAL_PHONE;
    
    	if($value[0]{0} == '+'){
    		$test_value = substr($value[0],1);
    	}else{
    		$test_value = $value[0];
    	}
    
    	if(is_numeric($test_value) && (strlen($test_value) > 3)){
    		return true;
    	}else{
    		return $error_message;
    	}
    }


    replace it with this one:

    function validate_simple_phone($value){
    
    	$error_message = VAL_PHONE;
    
    	if($value[0]{0} == '+'){
    		$test_value = substr($value[0],1);
    	}else{
    		$test_value = $value[0];
    	}
    	$test_value = str_replace(" ","",$test_value);
    	if(is_numeric($test_value) && (strlen($test_value) > 3)){
    		return true;
    	}else{
    		return $error_message;
    	}
    }


    that should allow spaces.


    MachForm Founder

    Posted 16 years ago #
  3. philwareham
    Member

    That's great, thanks yuniar.
    Can I scrounge one more thing... is there any chance you could supply the above example but also including support for "-" character in the phone number?
    Thanks a lot,
    Phil

    Posted 16 years ago #
  4. yuniar

    The easiest way to allow "-" character would be this:

    function validate_simple_phone($value){
    
    	$error_message = VAL_PHONE;
    
    	if($value[0]{0} == '+'){
    		$test_value = substr($value[0],1);
    	}else{
    		$test_value = $value[0];
    	}
    	$test_value = str_replace(array(" ","-"),"",$test_value);
    	if(is_numeric($test_value) && (strlen($test_value) > 3)){
    		return true;
    	}else{
    		return $error_message;
    	}
    }


    MachForm Founder

    Posted 16 years ago #

RSS feed for this topic

Reply