Click to See Complete Forum and Search --> : ASP.NET button with client side validation script


oafc0000
12-24-2008, 08:05 AM
Hi All

I have a asp.net button with a server side click event attached. But before the button posts back I want to run a client side check to check if the content of a field is in the correct format. So I was hoping to set the onClientClick equal to a javascript function that runs a test and returns true or false.

It fails to postback on return being returned and displays void(0) in the bottom right hand corner.

If change the button to a linkbutton it works fine! Help!

Basic Example below..obviously the real code is more complicate....

Javascript:

function test() {
return true;
}

HTML:

<asp:Button runat="server" ID="lnkAddUpdateDevice" Text="Save new device" OnClientClick="return test()">
</asp:Button>

Any ideas? By the way...the onclick event is assigned in the page_init....

ryanbutler
12-25-2008, 12:20 PM
Don't you want to use a Regular Expression Validator to check the contents of the text field for the appropriate contents? I don't typically recommend using client side Java Script because visitors could have that turned off. If you use a server-side control to check for appropriate content then that way you know it will always fire. In the Regular Expression Validator there's a property-value pair called ValidationExpression and then you fill the value with the Reg Ex.

oafc0000
12-26-2008, 02:49 PM
Don't you want to use a Regular Expression Validator to check the contents of the text field for the appropriate contents? I don't typically recommend using client side Java Script because visitors could have that turned off. If you use a server-side control to check for appropriate content then that way you know it will always fire. In the Regular Expression Validator there's a property-value pair called ValidationExpression and then you fill the value with the Reg Ex.

I am trying to validate a UK telephone number. If anyone has a regular experison that can do the job then cool but I have been unable to find one that is 100% perfect. I have a javascript function that is perfect.

ryanbutler
12-27-2008, 10:40 AM
What pattern of a UK phone number are you testing for? If it's similar to the US, writing one is fairly easy, because it's usually 3 digits for area code, followed by a dash, 3 digits, followed by a dash and then 4 digits. And just out of curiosity, if the link button works, why are you unable to use that?

Nate1
01-06-2009, 08:01 PM
presonally I would assign the validation to the button via the server on the loading of the button;

something like setbuttons

me.btnForValidation.onClientClick = "Return validatenumber(" & me.txtPhone.ClientID & ")"

<asp:button onclientClick='Return validatenumber(txtbox.clientID)' runat="server" id="btnForValidation" />

function validatenumber(objID){
//if matches regular expression
var phNUm=document.getElementbyID(objID);

//Test Reg Exp here via phNum.value;
return true;
//Would seem if not correct you would want to pass an error message to a error handler

else return false;
}


good Idea for error handling, create a div, then a function which will handle any messages thrown at the error handler, so you could have separate functions to check emails, phone numbers, names or whatever and could call throwError(msg) which will print the item to the Div (could be developed easily out of a Customwebcontrol, then dropped on any form page)

Hope that makes sense, quite easy to do, if you build it properly you should be able to reuse many times.