Martius Umali Martius Umali - 1 year ago 56
Javascript Question

validate textbox without postback

Good morning/afternoon/evening to all of you, Sir/Ma'am, I have a question regarding my code below. What my code does is to validate the textbox without postback. What I wanted to do is to show alert "hello" if the textbox has a value of "set". I'm truly grateful if you do reply and answer my question. I'm really sorry if my code below is kind of rubbish because I'm new to javascript and jquery.

$(document).ready(function () {
var fset = document.getElementById('<%=fname.ClientID%>').value;
if(fset.value=="set"){alert("hello");}
else{}
});

Answer Source

You either do the validation on form submit, onblur of the field or on keyup on the field. There other places that I'm not thinking of that you could do the client side as well.

Here's a version of on key up:

$(document).ready(function() {
     $("#<%=fname.ClientID%>").on("keyup", function() {
           if ( $(this).val() == "set" ) {
               alert("hello");
           }
     });
});

If you have a form you could do:

$(document).ready(function() {
     $("form").on("submit", function() {           
           if ( $("#<%=fname.ClientID%>").val() == "set" ) {
               alert("hello");
           }
     });
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download