Krystian Kwiatos Posted August 16, 2021 Share Posted August 16, 2021 Cześć, w jaki sposób mogę włączyć walidację numeru telefonu podczas składania zamówienia? Włączyłem wymóg jego wprowadzenia jednak można tam wpisać cokolwiek i przejdzie a chciałbym aby wymagało dziewięciu cyfr. Link to comment Share on other sites More sharing options...
0 Krystian Kwiatos Posted August 16, 2021 Author Share Posted August 16, 2021 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 More sharing options...
0 endriu107 Posted August 16, 2021 Share Posted August 16, 2021 W klasie Validate jest metoda isPhoneNumber i w niej możesz dokonać zmian lub bezpośrednio na inpucie w szablonie możesz zrobić walidację w js. Coś podobnego tylko dla pola adres mam jako poradnik na moim kanale YouTube. 1 Link to comment Share on other sites More sharing options...
0 Krystian Kwiatos Posted August 16, 2021 Author Share Posted August 16, 2021 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 More sharing options...
0 endriu107 Posted August 16, 2021 Share Posted August 16, 2021 Wystarczy sprawdzić czy wartość jest typu liczbowego integer oraz drugi warunek sprawdzić długość czyli lenght. 1 Link to comment Share on other sites More sharing options...
0 Krystian Kwiatos Posted August 16, 2021 Author Share Posted August 16, 2021 public static function isPhoneNumber($number) { return (preg_match('/^[+0-9. ()-]*$/', $number) AND strlen($number) == 9); } Znalazłem też coś takiego jako kod do dodania w validate.php jednak wyrzuca error strony Próbowałem też użyć tego - jednak error jest nadal taki sam. Link to comment Share on other sites More sharing options...
0 Krystian Kwiatos Posted August 16, 2021 Author Share Posted August 16, 2021 if (address.value.match("!/^([0-9]{9})$/")) { W twoim kodzie dodałem takie coś jednak nie działa, pewnie zrobiłem coś źle. Link to comment Share on other sites More sharing options...
0 endriu107 Posted August 16, 2021 Share Posted August 16, 2021 if (!isNaN(address.value) && (address.value.lenght == 9)) Spróbuj coś takiego, nie testowałem. 1 Link to comment Share on other sites More sharing options...
0 Krystian Kwiatos Posted August 16, 2021 Author Share Posted August 16, 2021 Niestety bez zmian, nie wymaga 9 cyfr Link to comment Share on other sites More sharing options...
0 ComGrafPL Posted August 16, 2021 Share Posted August 16, 2021 Sprawdź ten kod: function validate_isPhoneNumber(s) { var reg = /^[+0-9. ()-]+$/; return (reg.test(s) && s.length == 9); } Link to comment Share on other sites More sharing options...
0 endriu107 Posted August 16, 2021 Share Posted August 16, 2021 53 minutes ago, endriu107 said: if (!isNaN(address.value) && (address.value.lenght == 9)) Spróbuj coś takiego, nie testowałem. Ten kod również by zadziałał ale jest w nim literówka powinno być length a nie lenght, teraz zauważyłem 😅 Link to comment Share on other sites More sharing options...
0 robi1976 Posted January 3 Share Posted January 3 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 More sharing options...
0 endriu107 Posted January 3 Share Posted January 3 Rozwiazanie masz powyżej w tym poscie: tylko tam jest od 9 do 11 a ty możesz to uprościć pozostawiając tylko wartość 9 Link to comment Share on other sites More sharing options...
0 robi1976 Posted January 3 Share Posted January 3 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; } } Link to comment Share on other sites More sharing options...
0 endriu107 Posted January 3 Share Posted January 3 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 More sharing options...
0 robi1976 Posted January 3 Share Posted January 3 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 More sharing options...
Question
Krystian Kwiatos
Cześć, w jaki sposób mogę włączyć walidację numeru telefonu podczas składania zamówienia? Włączyłem wymóg jego wprowadzenia jednak można tam wpisać cokolwiek i przejdzie a chciałbym aby wymagało dziewięciu cyfr.
Link to comment
Share on other sites
15 answers to this question
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now