Skip to content
Related Articles

Related Articles

Improve Article
How to check empty/undefined/null string in JavaScript?
  • Last Updated : 15 Apr, 2019

If we want to check any string either it is filled or empty we can use Javascript for such purpose for example : form validation.
Example :

    <!--function to check that name can not be empty-->
        function checkempty(form) {
            if ( == null || 
       == undefined ||
       == 0) {
                alert("Name cannot be empty\n");
                return false;
            } else {
                alert("Your response has been recorded\n");
                return true;
            <form onsubmit="return checkempty(form1)"
                  action="#" method="post"
                <label>Enter your name</label>
                <input type="string"
                       placeholder="Enter name"
                       name="name" />
                <label>Enter Email</label>
                <input type="" 
                       placeholder="Enter email"
                       name="email" />
                <label>Enter password</label>
                <input type="password" 
                       placeholder="Enter password "
                       name="password1" />
                <label>Confirm password</label>
                <input type="password"
                       placeholder="Enter password " 
                       name="password2" />
                <input type="submit" 
                       value="submit" />

If the name is empty:

After submitting the form:

My Personal Notes arrow_drop_up
Recommended Articles
Page :