I want to check the textbox value before postback occurs. I set the onClientClick value to my function but I don’t know how to pass the data to check, in this case I want to check the txt1 entered text.
<asp:textbox id="txt1" runat="server />
<asp:LinkButton ID="LinkButton1"
runat="server"
Text="Save"
onclick="Save"
OnClientClick="javascript:check(WANT TO PUT TEXTBOX VALUE HERE);"
/>
My javascript:
function check(txt) {
var pattern = /^[0-9]{1,11}(,[0-9]{0,2})?$/;
if (!pattern.test(txt)) {
return false;
}
else {
return true;
}
}
The issue is that this check function is used along the keypress event of the txt1 so I cant use:
function check(){
var txt=$('#txt1').val();
}
The whole JS code:
$('#txt1').keypress(function (e) {
var key = String.fromCharCode(e.which);
var txt = $(this).val() + key;
if (check(txt)) {
// do sth
} else {
e.preventDefault();
// do sth
}
});
How can I tell the OnclientCLick function to get the txt1 value?
Thank you.
Something like that :
Edit:
Other methods :
You can add it on server side :
LinkButton1.OnClientClick="check(document.getElementById('" + txt1.ClientID + "').value;)";If you want to remain in the aspx page, you have to put a name of a javascript function in the OnClientClick and implement the javascript function in a script tag :