Wednesday, 15 October 2025

Show Hide TextBox on check uncheck CheckBox using JavaScript and jQuery

Introduction: Hello Guys, “Welcome back” Today, I am here with another one new great article. In this article I explained how to display or show and hide or invisible HTML DIV with TextBox when CheckBox will be checked and unchecked or selected and unselected by using JavaScript as well as jQuery. When user will click on the CheckBox based on whether it is checked (selected) or unchecked (unselected), the HTML DIV with TextBox will be show or hide.

Join our Channel  on Whtsapp and Telagram for All Updates                                         

Display Hide DIV with TextBox on CheckBox checked unchecked using JavaScript

On the HTML page I will place two controls and a div, first control will be CheckBox with the id named chk_question and an HTML DIV with the id named div_question and inside this div I will place a TextBox with the id named txt_question. OnClick event handler of the CheckBox I will assign JavaScript function named display_hide_div. When user will click on the CheckBox, the display_hide_div JavaScript function will be executed. In this function, based on whether CheckBox is checked (selected) or unchecked (unselected), the HTML DIV with TextBox will be show or hide.

 

<script type="text/javascript">

    function display_hide_div (chkPassport) {

        var divquestion = document.getElementById("div_question");

        divquestion.style.display = chkquestion.checked ? "block" : "none";

    }

</script>

<label for="chkquestion">

    <input type="checkbox" id="chk_question" onclick=" display_hide_div (this)" />

    Have you any questions?

</label>

<hr />

<div id="div_question" style="display:none">

    Question:

    <input type="text" id="txt_question" />

</div> 

You can copy and paste the whole code on your HTML page with JavaScript that I mentioned below to enjoy the execution of the program.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title></title>

    <style type="text/css">

        body

        {

            font-family: Arial;

            font-size: 10pt;

        }

    </style>

</head>

<body>

    <script type="text/javascript">

        function display_hide_div(chk_question) {

            var dvquestion = document.getElementById("div_question");

            dvquestion.style.display = chk_question.checked ? "block" : "none";

        }

    </script>

    <label for="chk_question">

        <input type="checkbox" id="chk_question" onclick="display_hide_div(this)" />

        Do you have any Question?

    </label>

    <hr />

    <div id="div_question" style="display: none">

        Question:

        <input type="text" id="txtPassportNumber" />

    </div>

</body>

</html>

Show Hide DIV with TextBox when CheckBox is checked unchecked using jQuery

On the HTML page I will place two controls and a div, first control will be CheckBox with the id named chk_question and an HTML DIV with the id named div_question and inside this div I will place a TextBox with the id named txt_question. Here I also inherited the jQuery file <script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">

</script> The CheckBox has been assigned a jQuery OnClick event handler. When user will be clicked on CheckBox, based on whether CheckBox is checked (selected) or unchecked (unselected), the HTML DIV with TextBox is shown or hidden.

<script type="text/javascript">

    $(function () {

        $("#chk_question").click(function () {

            if ($(this).is(":checked")) {

                $("#div_question").show();

             }else {

                $("# div_question").hide();

            }

        });

    });

</script>

<label for="chkquestion">

    <input type="checkbox" id="chk_question" />

    Have you any questions?

</label>

<hr />

<div id="div_question" style="display:none">

    Question:

    <input type="text" id="txt_question" />

</div> 

You can copy and paste the whole code on your HTML page with jQuery that I mentioned below to enjoy the execution of the program.

<body>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

    <script type="text/javascript">

        $(function () {

            $("#chk_question").click(function () {

                if ($(this).is(":checked")) {

                    $("#div_question").show();

                } else {

                    $("#div_question").hide();

                }

            });

        });

    </script>

    <label for="chk_question">

        <input type="checkbox" id="chk_question" />

        Do you have any Question?

    </label>

    <hr />

    <div id="div_question" style="display: none">

        Question:

        <input type="text" id="txt_question" />

    </div>

</body>

</html>

Conclusion: In above code, I explained how to display or show and hide or invisible HTML DIV with TextBox when CheckBox will be checked and unchecked or selected and unselected by using JavaScript as well as jQuery. Bye and take care of you Developers. We will come back again shortly with the new article.

Regards

Programming Hub


 

Tuesday, 14 October 2025

Passport Number validation using JavaScript

 Introduction: Hello Guys, “Welcome back” Today, I am here with another one new great article. In this article I explained how to implement Indian Passport Number validation using Regular Expression (Regex) using JavaScript.

Join our Channel  on Whtsapp and Telagram for All Updates                                          

Code for HTML Page

On the  HTML page I will be placed three control one will be  TextBox with the id named txt_passport_number,  second control will be SPAN with the id named lbl_error and a Button with the id named btn_submit. On the OnClick event of the Button i assigned a JavaScript function named validate_passport_number().

Passport Number:

<input type="text" id="txt_passport_number" class="passport" />

<span id="lbl_error" class="error"></span>

<br/>

<input type="button" id="btn_submit"  value="Submit" onclick="validate_passport_number()" />

Validating Passport Number using Regular Expression in JavaScript

When user will be click on the Submit Button then the validate_passport_number() JavaScript function will be executed. Inside the ValidatePassportNumber JavaScript function, the Passport Number TextBox will be referenced by using the id of the textbox and its value will be tested by using a Regular Expression.

The below mentioned conditions must satisfy for the Passport Number to be termed as valid.

1. The Passport Number should be eight characters long.

2. The first character  of Passport Number should be an upper case alphabet with A-Z excluding Q, X, and Z.

3. The second character of Passport Number should be any digit. 1-9.

4. The third character of Passport Number should be any digit. 0-9.

5. The next character of Passport Number should be zero or one white space character.

6. The next four characters of Passport Number should be any digit. 0-9.

7. The last character Passport Number should be any digit. 1-9.

Examples: B3185320,  B38 92068

<script type="text/javascript">

    function validate_passport_number() {

        var passportNumber = document.getElementById("txt_passport_number").value;

        var lbl_msg = document.getElementById("lbl_error");

       lbl_msg.innerHTML = "";

        var expr = /^[A-PR-WY][1-9]\d\s?\d{4}[1-9]$/;

        if (!expr.test(passportNumber)) {

           lbl_msg.innerHTML = "Please enter valid Passport Number.";

        }

    }

</script>


CSS Class

The following CSS classes are used.

1. error – It will apply RED color to the error message.

2. passport – It will force the letters to be UPPER case.

<style type="text/css">

    body { font-family: Arial; font-size: 10pt; }

    .error { color: Red; }

    .passport { text-transform: uppercase; }

</style>

Conclusion: In above code, I explained how to implement Indian Passport Number validation using Regular Expression (Regex) using JavaScript. We will come back again shortly with the new article.

Regards

Programming Hub