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:

user3262364
  • 369
  • 3
  • 9
  • 23

3 Answers3

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

Akshey Bhat
  • 8,227
  • 1
  • 20
  • 20
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;

Community
  • 1
  • 1
Jaqen H'ghar
  • 16,186
  • 8
  • 49
  • 52
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;
}

Moussa Khalil
  • 635
  • 5
  • 12