This question already has an answer here:
I have the follwing code in PHP:
if(is_numeric($first_name[0]) === true){
//Do something
}
How would I be able to do the same check using JavaScript? I also would like to get a PHP script to check if there is a number in $first_name at all, as I don't want the user to add a number in their first or last names please?
</div>
if (/-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/.test(yourInput)) {
// Do something
}
Of course, this will only work on strings. For a different method, see a duplicate question:
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
To only check if the input contains a number (0-9) at all, regex works again:
if (/[0-9]+/.test(yourInput)) {
// Do something
}
If the variable in question is $first_name
if( $first_name && !isNaN($first_name[0])) {
// do something
}
in js to check if the number variable is a number:
isFinite(number) && !isNaN(parseFloat(number))
Something I found here:
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
If you want to check whether *$first_name* contains any digits, then:
if (/\d/.test($first_name)) {
// $first_name contains at least one digit
}