All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Check For Null Or Empty String

Last Updated : Mar 11, 2024

JavaScript Check For Null Or Empty String

In this tutorial we will show you the solution of JavaScript check for null or empty string, here we used built-in values in javascript (i.e) null, undefined and empty quotes.

Those are values compared with user input values so we can easily found out whether input field is empty or null then we have instruct user to fill correct input and can’t leave as empty input field so user can give correct input.

Step By Step Guide On JavaScript Check For Null Or Empty String :-

Here we defined two input tags for getting string input from user and submit button defined for submit user input value then defined one div tag with id ‘h’ for displaying some instruction for user on webpage.

In script we defined function fun(), this is appends with onclick attribute in submit button so it will called when user clicks on it.

Here we collecting user inputs by id ‘str’ then stored on variable ‘inpt’ and using if condition we checks whether user input is empty or not.

We appended respective defined program instruction message on webpage by innerHTML for user.

<!DOCTYPE html>
<html>
    <head>
        <title>CHECKING UNDEFINED/EMPTY/NULL</title>
    </head>
    <body>
        <input id="str" type="text" placeholder="Enter a String">
        <input onclick="fun()" type="submit" value="Submit">
        <div id="h"></div>
        <script>
            function fun(){
                var inpt=document.getElementById('str').value;
                if(inpt == null || inpt==undefined || inpt==''){
                    document.getElementById('h').innerHTML="We can't leave as empty<br>";
                }
                else{
                document.getElementById('h').innerHTML="Response received";
                }
            }
        </script>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Here we defined two input tags for getting input string from user and submit button for verify user submissions when user clicks on submit button by function ‘fun()’.
  7. Div tag defined with id attribute ‘h’. In script we defined function fun(), here we collected user entered input and stored on variable ‘inpt’. Using if condition we checks whether entered input is ‘null, undefined or empty’.
  8. If condition true means displaying message to user for entered valid string otherwise displays user response received message.
  9. We appended respective verified message on div element ‘h’ and it will displayed on webpage.
  10. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to check whether input is null or empty using javascript.

When we executes program on browser we can see the input box with submit button.

User needs to give input string and clicks on submit button then user input values checked using if condition.

If returns true means displays ‘We can't leave as empty’ message so user will not leave them as empty or null otherwise it displays message ‘Response received’ on webpage for user verification entered inputs are received message.

I hope this tutorial on JavaScript check for null or empty string helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪