Javascript 如何 return 如果名为 'payroll_no'(20 长字符串)的变量仅包含数字则为真,如果它包含任何字符则为假

Javascript how to return True if a variable called 'payroll_no' (20 long string) contains only numerics and false if it has any characters

if  (!Character.isDigit(payroll_no))

{

   document.getElementById('field_117219').value ='We do not have your payroll number please email it to us')

}

else

 {

    Name = document.getElementById('field_115676').value;

    document.getElementById('field_117219').value ="Appraisal Details for "+Name;

     iframe_115667.location.replace('/sorce/apps/enh/asp/Appraisals.aspxgridname=Appraisals&Payroll_no='+Payroll_no);


}

您可以对其应用正则表达式:

if (/^\d+$/.test(payrol_no)) {
    // Just digits
} else {
    // contains non-digit characters
}

只需使用isNaN(payrol_no)。它 returns false 如果它是一个数字(因此 "is Not A Number"),否则 true