Validate only numbers in a text field (I would like to validate it using javascript code)

How to validate only numbers in a text field?

If you are referring to a text user input, then you’ll need to change a type of your input from Text to Number in the settings:

That’s right, I know i can, but I would like to validate it using javascript code

@mizaeltovarreyes ,

$w.onReady(() => {
    $w("#input1").onChange(event => {
        if(isNaN($w("#input1").value)){
            //do if it's not a number
        } else {
            //do if number
        }
    })
})

and of course, if you want you can put it in custom validation:

$w("#input1").onCustomValidation( (value, reject) => {
  if(isNaN(value)) {
    reject(`"${value}" is not a number`);
  }
} );

@jonatandor35 Thank you very much, but it throws me error, can you tell me what is wrong with my code?

export function input17_change(event) {

$w("#input17").onCustomValidation( (value, reject) => { 
**if** (isNaN(value) { 
reject(`"${value}" is not a number`); 

}
} );
}

@mizaeltovarreyes yes. close the parenthesis:
if(isNaN(value) ) {

1 Like

@jonatandor35 Many thanks!!

 export function input17_change(event) {     $w("#input17").onCustomValidation( (value, reject) => {  if(isNaN(value)) {reject(`"${value}" is not a number`); } }); }