Problem entering only numbers in TextInput - Forum

Forum Navigation
You need to log in to create posts and topics.

Problem entering only numbers in TextInput

1- Hello and good time to all my dear friends! Is there a way to enter only numbers in TextInput? And no text is recorded!
2- The number is registered in NumericInput! But when we go over it with the mouse! If the registered number is a decimal number! A small message will appear! That I do not like to have this message! Thank you for any advice.
Thanks

Uploaded files:
  • You need to login to have access to uploads.

The code example I was looking for.
I would be happy if dear friends, if they have a better example, share it here. Thanks

Uploaded files:
  • You need to login to have access to uploads.

Hi, Just hide the numeric input's spinner, You will not see any message,

paste this code in project>properites>styles:

/* For Webkit browsers like Safari and Chrome */
input[type="number"]::-webkit-inner-spin-button, 
input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none; 
  margin: 0; 
}

/* For Firefox */
input[type="number"] {
  -moz-appearance: textfield;
}

 

javadrajabihakami has reacted to this post.
javadrajabihakami

@javadrajabihakami you can use slAllowedChars command to allow only numeric characters into any TextInput object-

javadrajabihakami has reacted to this post.
javadrajabihakami
Quote from luishp on January 4, 2024, 9:14 pm

@javadrajabihakami you can use slAllowedChars command to allow only numeric characters into any TextInput object-

Unfortunately, using a mobile phone, I can fill in the fields that are only set to enter numbers with any type of letters! Is this code only for computers? Or I have not used it correctly! Of course, it works well on the computer. Thanks

@javadrajabihakami please try this code and let me know if it works:

BeginJS
  const inputElement = document.getElementById('TextInput1');
  inputElement.addEventListener('input', function(event) {
    const inputValue = event.target.value;
    const allowedChars = /^[Z0-9\s]+$/;
    if (!allowedChars.test(inputValue)) {
      event.target.value = inputValue.replace(/[^Z0-9\s]+/g, '');
    }
  });
EndJS

It allows to restrict the allowed characters based in a regular expression.

To customize the regular expression /^[a-zA-Z0-9\s]+$/ and restrict or allow different characters in your HTML input field, you can modify it based on your specific requirements. Here's how you can customize it:

  1. Allow Specific Characters: If you want to allow specific characters, simply add those characters inside the square brackets []. For example, if you want to allow lowercase letters, digits, hyphens, and underscores, you can use [a-z0-9\-_]+.
  2. Allow Special Characters: If you want to allow certain special characters, you can add them within the square brackets. Be careful with special characters, as some of them may have special meanings in regular expressions. You may need to escape them using a backslash (\) if necessary. For example, to allow only letters, digits, hyphens, underscores, and periods, you can use [a-zA-Z0-9\-_.]+.
  3. Disallow Specific Characters: If you want to disallow certain characters, you can exclude them from the character class. For example, if you want to allow all characters except the exclamation mark and the dollar sign, you can use [^!$]+.

Here are a few examples:

  • Allow lowercase letters and digits only: /^[a-z0-9]+$/
  • Allow letters, digits, hyphens, and underscores: /^[a-zA-Z0-9-_]+$/
  • Allow letters, digits, and the "@" symbol (for email-like input): /^[a-zA-Z0-9@]+$/

Customize the regular expression according to your specific requirements by adjusting the characters within the square brackets and any necessary escape characters.

javadrajabihakami has reacted to this post.
javadrajabihakami

Hello and good time! Thanks dear friend !

Yes it works well and it's what I'm looking for!

But I wish he didn't accept the distance.

thanks again

@javadrajabihakami Use this one:

BeginJS
const inputElement = document.getElementById('TextInput1');
inputElement.addEventListener('input', function(event) {
  const inputValue = event.target.value;
  const allowedChars = /^[0-9]+$/;
  if (!allowedChars.test(inputValue)) {
    event.target.value = inputValue.replace(/[^0-9]+/g, '');
  }
});
EndJS

 

javadrajabihakami has reacted to this post.
javadrajabihakami
Quote from luishp on January 7, 2024, 6:36 pm

@javadrajabihakami Use this one:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
BeginJS
const inputElement = document.getElementById('TextInput1');
inputElement.addEventListener('input', function(event) {
const inputValue = event.target.value;
const allowedChars = /^[0-9]+$/;
if (!allowedChars.test(inputValue)) {
event.target.value = inputValue.replace(/[^0-9]+/g, '');
}
});
EndJS
BeginJS const inputElement = document.getElementById('TextInput1'); inputElement.addEventListener('input', function(event) { const inputValue = event.target.value; const allowedChars = /^[0-9]+$/; if (!allowedChars.test(inputValue)) { event.target.value = inputValue.replace(/[^0-9]+/g, ''); } }); EndJS
BeginJS
const inputElement = document.getElementById('TextInput1');
inputElement.addEventListener('input', function(event) {
  const inputValue = event.target.value;
  const allowedChars = /^[0-9]+$/;
  if (!allowedChars.test(inputValue)) {
    event.target.value = inputValue.replace(/[^0-9]+/g, '');
  }
});
EndJS

 

thanks my dear friend