1

my input text box source like this:

<asp:TextBox ID="TxtDepCode" runat="server" Height="18px"></asp:TextBox>

and my javascript function like this:

  <script type="text/javascript" language="javascript">
    function confirm_user() {
        var userPass = document.getElementById('TxtDepCode');
        alert(userPass)
        if (userPass=''){
        alert("value is blank")
        }
        if (confirm("Department already available, would you like to update ?") == true)
            return true;
        else
            return false;
    }
</script>

in submit button click i want to check wethar corresponding text is empty or not my submit button click event like this:

1
  • 1
    try userPass.value Commented Jun 5, 2016 at 12:08

3 Answers 3

1
var userPass = document.getElementById('TxtDepCode').value;

Or

var userPass = document.getElementById('<%=TxtDepCode.ClientID%>').value;

Modify the first line of function to get value of textbox as above

if (userPass==''){

Modify if as above

Sign up to request clarification or add additional context in comments.

6 Comments

Modify your if statement also as given in modified answer
var userPass = document.getElementById('TxtDepCode').value; alert(userPass)
i am just cheking wethar i am getting my input value in that variable..but not getting
now got it sir.if you dont mind y i want to add clientID
No need if you got it. You need to use client id in case textbox is nested in some other control.
|
1

The crucial issues in the code are as follows:

  1. Get the value of the password and not the element:

    var userPass = document.getElementById('TxtDepCode').value;

  2. Change the if to == or === instead of single =

    if (userPass == '') {

  3. You are using <asp:TextBox> that can have a dynamic ID. Therefore you should get the dynamic ID using .NET's ClientID:

    var userPass = document.getElementById('<% =TxtDepCode.ClientID %>').value;

Comments

0

You need to return false if value is blank too

<script type="text/javascript" language="javascript">
function confirm_user() {
    var userPass = document.getElementById('TxtDepCode');
    alert(userPass)
    if (userPass.value == ''){ // == not = and userPass.value
    alert("value is blank");
    return false; // Need to stop the function
    }
    if (confirm("Department already available, would you like to update ?") == true)
        return true;
    else
        return false;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.