Skip to content

Over-input... (no max length limit depending on selected country) #1981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
NDruce opened this issue Mar 25, 2025 · 2 comments
Open

Over-input... (no max length limit depending on selected country) #1981

NDruce opened this issue Mar 25, 2025 · 2 comments

Comments

@NDruce
Copy link

NDruce commented Mar 25, 2025

Hi. If phone number is like +375 (33) 444-55-66,
people are not restricted to input (33) 4445566778899423894372856325.

The same with ALL the countries over the world

Strict mode does not help. It works only if remove country auto-detecting.

Please, fix it

PS: Set maxlength="..." will not help. Cuz different countries has different phone lengths.

PSS: Hmm, I invented the crutch! xD
Set maxlength to placeholder string length :D

@NDruce
Copy link
Author

NDruce commented Mar 25, 2025

How to listen intl-tel-input init event?

@NDruce
Copy link
Author

NDruce commented Mar 25, 2025

Workarounded it

<div class="col-xs-12 col-md-6">
	<div class="form-group form-floating mb-2">
		<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/intl-tel-input@25.3.0/build/css/intlTelInput.css">

		<label for="phone-number">Phone</label>
		<input type="tel" class="form-control" id="phone-number"{* placeholder="+_ (___) ___-____"*} required minlength="12" maxlength="24">

		<script src="https://cdn.jsdelivr.net/npm/intl-tel-input@25.3.0/build/js/intlTelInput.min.js"></script>
		<script>
			const phoneInput = document.querySelector("#phone-number");
			if (phoneInput && phoneInput.id) {
				window.intlTelInput(phoneInput, {
					strictMode: true,
					separateDialCode: true,
					initialCountry: "auto",
					geoIpLookup: callback => {
						fetch("https://ipapi.co/json")
							.then(res => res.json())
							.then(data => callback(data.country_code))
							.catch(() => callback("us"))
						;
					},
					loadUtils: () => import("https://cdn.jsdelivr.net/npm/intl-tel-input@25.3.0/build/js/utils.js")
				});
				function setPhoneMaxLength() {
					let placeholder = phoneInput.getAttribute('placeholder');
					if (placeholder) {
						phoneInput.setAttribute('maxlength', (placeholder.length).toString());
					} else {
						phoneInput.removeAttribute('maxlength');
					}
				}
				$('#' + phoneInput.id)
					.removeAttr('maxlength')
					.change(function () {
						if (!$(this).attr('maxlength')) {
							setPhoneMaxLength();
						}
					})
				;
				phoneInput.addEventListener("open:countrydropdown", () => {
					phoneInput.removeAttribute('maxlength');
				});
				phoneInput.addEventListener("close:countrydropdown", () => {
					phoneInput.removeAttribute('maxlength');
					setPhoneMaxLength();
				});
				phoneInput.addEventListener("countrychange", () => {
					phoneInput.removeAttribute('maxlength');
					setPhoneMaxLength();
				});
			}
		</script>
	</div>
</div>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant