Getting a integer value from a textbox, how to check if it's NaN or null etc?

后端 未结 4 1315
我寻月下人不归
我寻月下人不归 2021-01-05 06:46

I am pulling a value via JavaScript from a textbox. If the textbox is empty, it returns NaN. I want to return an empty string if it\'s null, empty, etc.

4条回答
  •  旧时难觅i
    2021-01-05 07:14

    One thing you could do is a regex check on the value of the textbox and make sure it fits the format of an accepted number, and then if it fits the format perform your process, otherwise return an empty string.

    Edit: This is an example from some code I have in front of me (might not be the best regular expression):

    var anum=/(^\d+$)/;
    
    if (!anum.test(document.getElementById("<%=txtConceptOrderValue.ClientID %>").value))
    {
        alert("Order Value must be a valid integer");
        document.getElementById("<%=txtConceptOrderValue.ClientID %>").focus();
        return false;
    }
    

    Edit 2: I should also note that I am using ASP.NET which is why I have the slightly funky way of accessing the textbox. In your regular JavaScript case it may not be as cluttered.

提交回复
热议问题