I would like a text field that only accepts numerical and sign values for example $1,300 and if they put letters the text is automatically deleted.
You can do something like:
$w.onReady(() => {
$w("#input1").onKeyPress(event => {
setTimeout(() => {validateInput();}, 10)
})
function validateInput(){
let value = $w("#input1").value;
if (/[a-z]/.test(value.toLowerCase())){
$w("#input1").value = "";
};
}
})
I have the following function, It is to enter only numbers and signs, but I would like that when they enter letters the field is automatically deleted
function formatNumber(num) {
return “$” + num
.toFixed(2)
.replace(/(\d)(?=(\d{3})+(?!\d))/g, “$1,”);
}
export function input2_change(event) {
$w(‘#input2’).value = formatNumber(Number($w(‘#input2’).value));}
The code is only for entering numbers and signs, and that the numbers are automatically aligned
for example $1, but I would like that when letters are entered, the field is automatically deleted.
With this code, when you enter letters in the text field instead of deleting it, NaN appears and the field is not deleted.
@mizaeltovarreyes first of all, I’d recommend instead of using your formatNumber( ) function, to use the JavaScript built-in method for formatting currency. For example:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
(paragraph: Using Options).
But I don’t realy understand what would you like to get if the user types in the “$” symbol.
@jonatandor35 That’s right, I would like to delete the text field when writing words
@jonatandor35
Currently, if you type words, the text field is set to default Nan and I would like the text field to be deleted when writing words.
sorry for my english.
@mizaeltovarreyes yes. i got that. But what I’m asking is what would you like to do if the user types: “$2,000”. It’s a string, but I’m not sure you wanted to delete it.