mike55 Posted March 9, 2006 Posted March 9, 2006 Hi all I am attempting to use javascript to validate the data submitted by the user. I have a single textbox, a custom validator, and a validation summary control on my page. User should be able to place a numeric value in the textbox (I'm ignoring alpha characters at the moment), if the value is between 2 and 3, then the value is correct, otherwise I want to set the custom validator to false and display the error message in the validation summary. I have succeed in displaying the error in a javascript alert box, but I am unsure on how to move on with the custom validator. Here is the code I am using so far... function ValidateData(){ var data = document.getElementById("TextBox1"); if (data.value < 1 || data.value > 3){ alert("Invalid Data"); } else{ return true; } } Mike55. Quote A Client refers to the person who incurs the development cost. A Customer refers to the person that pays to use the product. ------ My software never has bugs. It just develops random features. (Mosabama vbforums.com)
rfazendeiro Posted March 9, 2006 Posted March 9, 2006 Well, if you want to validade a textbox with values between 2 and 3 u can use a RangeValidator. It comes something like this <asp:textbox id="textbox1" runat="server"/> <asp:RangeValidator id="valRange" runat="server" ControlToValidate="textbox1" MaximumValue="3" MinimumValue="2" Type="integer" ErrorMessage="* Only numbers between 2 and 3" Display="static">*</asp:RangeValidator> Quote
mike55 Posted March 10, 2006 Author Posted March 10, 2006 Well, if you want to validade a textbox with values between 2 and 3 u can use a RangeValidator. It comes something like this <asp:textbox id="textbox1" runat="server"/> <asp:RangeValidator id="valRange" runat="server" ControlToValidate="textbox1" MaximumValue="3" MinimumValue="2" Type="integer" ErrorMessage="* Only numbers between 2 and 3" Display="static">*</asp:RangeValidator> The range validator is grand, however I want to try and validate the majority of the user data on the client side. Unfortunately the level and type of broadband available to most in Ireland is limited, so the majority of people viewing the site are going to be using 56K dial-up. Therefore if I can use the javascript to validate all the data before the postback, I can make the site more accessible to users. I know that in some cases that I will have no alternative but to allow the postback, and I do not want to start dropping all sorts of validators around my page and setting the enableclientscript to true on them. Mike55. Quote A Client refers to the person who incurs the development cost. A Customer refers to the person that pays to use the product. ------ My software never has bugs. It just develops random features. (Mosabama vbforums.com)
rfazendeiro Posted March 10, 2006 Posted March 10, 2006 You can, with javascript, go through all the validatores that existe and see if they are valid. Here is the code. First of all we need to add an atribute to the button so that he runs our javascript function. To do that just put this line in the InitializeComponent method Button1.Attributes.Add("onclick","CheckValidatorErrors();"); then before the HEAd tag just insert this javascript <script language="JavaScript" type="text/javascript"> function ValidatorUpdateDisplay(val) { if (typeof(val.display) == "string") { if (val.display == "None") { return; } if (val.display == "Dynamic") { val.style.display = val.isvalid ? "none" : "inline"; return; } } val.style.visibility = val.isvalid ? "hidden" : "visible"; } function CheckValidatorErrors() { for(i=0;i< Page_Validators.length;i++) { if (!Page_Validators[i].isvalid) { Page_IsValid = false; ValidatorUpdateDisplay(Page_Validators[i]); } } } </script> the thing is i don't know if the server side code is executed idf you have this javascript code. Anyway hope this helps, at least to point you in a right direction Quote
Administrators PlausiblyDamp Posted March 10, 2006 Administrators Posted March 10, 2006 The validation controls will generate Javascript if the browser supports it - if not then it will just use server side validation (the server side validation happens regardless of client side script support). If you wish to use a customer validator your javascript function needs to have a particular signature (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconcustomvalidatorcontrol.asp). Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mike55 Posted March 10, 2006 Author Posted March 10, 2006 Many thanks guys. Will let you know how I get on. Quote A Client refers to the person who incurs the development cost. A Customer refers to the person that pays to use the product. ------ My software never has bugs. It just develops random features. (Mosabama vbforums.com)
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.