Jump to content
  • 0

Walidacja numeru telefonu przy zamówieniu


Krystian Kwiatos

Question

15 answers to this question

Recommended Posts

  • 0

w pliku validate.php rozwiązałem to w ten sposób i działa - 

public static function isPhoneNumber($number){
        $stripped = preg_replace('/[^0-9]/', '', $number);
        $len = strlen($stripped);
        $min = 9;
        $max = 11;
        if ($len >= $min && $len <= $max){
          return preg_match('/^[+0-9. ()-]*$/', $number);
        }else{
         return false;
        }
      }

Dziękuję @endriu107 za pomoc :)

Link to comment
Share on other sites

  • 0
let address = document.querySelector("input[name'phone']");
let addressBtn = document.querySelector("button[name'confirm-addresses']");

function ready() {
	if (addressBtn) {
		address.addEventListener('keyup', () => {
			if (address.value.match(".*\\d.*")) {
               addressBtn.removeAttribute('disabled','')
			   addressBtn.setAttribute('enabled','')
			} else {
				addressBtn.removeAttribute('enabled','')
				addressBtn.setAttribute('disabled','')
			}
		})
	}
}

document.addEventListener("DOMContentLoaded", ready);

Napisałem to w ten sposób jednak nie mam pojęcia w jaki sposób sprawdzać czy numer jest poprawny

Link to comment
Share on other sites

  • 0
On 8/16/2021 at 11:55 AM, endriu107 said:

Ten kod również by zadziałał ale jest w nim literówka powinno być length a nie lenght, teraz zauważyłem 😅

Witam
Jak zmienić poniższy kod żeby wymusić wpisywanie tylko 9 cyfr w numerze telefonu?

 

Quote

/**
     * Check for phone number validity.
     *
     * @param string $number Phone number to validate
     *
     * @return bool Validity is ok or not
     */
    public static function isPhoneNumber($number)
    {
        return preg_match('/^[+0-9. ()\/-]*$/', $number);
    }
 

 

Link to comment
Share on other sites

  • 0

dodałem poniższy kod
jednak coś preście nie pasuje :(

Quote

{
        $stripped = preg_replace('/[^0-9]/', '', $number);
        $len = strlen($stripped);
        $min = 9;
        if ($len >= $min && $len <= $max){
          return preg_match('/^[+0-9. ()-]*$/', $number);
        }else{
         return false;
        }
    }
 

 

Zrzut ekranu 2025-01-03 124054.png

Link to comment
Share on other sites

  • 0

Tak to nie zadziała ponieważ nie masz zdefiniowanego $max a sprawdzasz go w warunku :)

Spróbuj tak:

public static function isPhoneNumber($number){
        $stripped = preg_replace('/[^0-9]/', '', $number);
        $len = strlen($stripped);
        $digits = 9;
        if ($len == $digits){
          return preg_match('/^[+0-9. ()-]*$/', $number);
        }else{
         return false;
        }
}

 

Link to comment
Share on other sites

  • 0

dzięki, na innej grupie dostałem taką podpowiedź i działa

 

Quote

/**

* Check for phone number validity.

*

* @param string $number Phone number to validate

*

* @return bool Validity is ok or not

*/

public static function isPhoneNumber($number)

{

// Ensure the phone number contains exactly 9 digits

return preg_match('/^\d{9}$/', $number);

}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...