• Resolved Nishant Patel

    (@nishantpatel121)


    I want to verify phone number function that only be added up to 10 numbers and formate for that number should be 999-999-9999

    Can be able to find function for that validation with in plugin or settings

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    we do not have a phone number validator in the WPAdverts but it should be possible to add it by adding the code below in your theme functions.php file (or even better by creating new blank plugin and pasting it there https://wpadverts.com/blog/how-to-use-code-snippets-in-wordpress/).

    
    add_action( "init", function() {
        if( ! defined( "ADVERTS_PATH" ) ) {
            return;
        }
        adverts_form_add_validator("my_is_phone_number", array(
            "callback" => "my_is_phone_number",
            "label" => "Phone",
            "params" => array(),
            "default_error" => __( "Provided phone format is invalid", "wpadverts" ),
            "validate_empty" => false
        ));
    });
    add_filter( "adverts_form_load", function( $form ) {
        if( $form["name"] != "advert" ) {
            return $form;
        } 
        foreach( $form["field"] as $k => $f ) {
            if( $f["name"] == "adverts_phone" ) {
                $form["field"][$k]["validator"][] = array( "name" => "my_is_phone_number");
            }
        }
        return $form;
    });
    function my_is_phone_number( $data ) {
        if(preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/", $data)) {
            return true;
        } else {
            return "invalid";
        }
    }
    
    Thread Starter Nishant Patel

    (@nishantpatel121)

    Yes but is has to be require and set place holder for 999-999-9999

    Plugin Author Greg Winiarski

    (@gwin)

    To set a placeholder for the adverts_phone field above or below the line

    
    $form["field"][$k]["validator"][] = array( "name" => "my_is_phone_number");
    

    add

    
    $form["field"][$k]["placeholder"] = "999-999-9999";
    
    Thread Starter Nishant Patel

    (@nishantpatel121)

    thank you @gwin you are the great.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Phone Number Valication’ is closed to new replies.