简体   繁体   中英

How to handle alphanumeric and null in JavaScript?

We are facing an issue on handling null , because if the value is null , it is not reaching the server. Below is the code snippet:

<input ... onchange="return checkEmpty(this);" />

And the JavaScript:

function checkEmpty(value) {
    alert("Empty Check() "+value);
    if (myTrim(value.length == 0)) {
        alert("please Enter Value!"+ value +"   value");
        return false;
    }
    return true;
}

We are trying to display one popup for null value and the request should go to the server, but some exception is occurring we are unable to identify it and the request is not coming to server.

You can do this:

if (variable == null) {
     // do something 
}

--which is 100% equivalent to the more explicit but less concise:

if (variable === undefined || variable === null) {
     // do something 
}

While there are ways to solve this specific problem (and the other answer(s) manage to answer that), I'll try to address a more general one.

What you essentially want is the form control to not be empty. Well, you don't need JavaScript for that at all

<input ..... required>

That will prevent the form from submitting unless the required field was filled.

 <form> Try to submit me empty! <input required> <button>I dare you!</button> </form> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM