Validator for RadNumericTextBox created dynamically

2 posts, 0 answers
  1. iomega 55
    iomega 55 avatar
    64 posts
    Member since:
    Dec 2004

    Posted 23 Aug 2009 Link to this post

    Hi:

    I'm creating codebehind a radnumerictextbox, and I need to have mandatory a positive value between 0 and 100. I found if a set you can still leave empty the textbox and radnumeric doesnt complaint about it, also you can write the negative sign (-) and it permits you write it

    - how do I need to configure in my radnumeric for complaint when I have an empty value
    - how do I need to configure in my radnumeric for complaint when I write a negative sign
    - how can I create a dynamic custom o request validator in the codebehind for a radnumeric textbox
    - does the radnumeric have an attribute or method for defining a message when it has an error or warning


    Thanks in advance.
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 24 Aug 2009 Link to this post

    Hello,

    Try the following approach and see whether it suits your need.

    - Added OnKeyPress event in order to prevent the negative sign(-) from entering into the textbox.
    - Attach OnError client event for displaying the error message when it has an error or warning.
    - Added RequireFieldValidator for ensuring that the textbox has value when page is submitted.

    C#:
     
        RadNumericTextBox textbox = new RadNumericTextBox(); 
        textbox.ID = "textbox1"
        textbox.MinValue = 0; 
        textbox.MaxValue = 100; 
        textbox.ClientEvents.OnError = "OnError"
        textbox.ClientEvents.OnKeyPress = "KeyPress"
        this.form1.Controls.Add(textbox); 
     
        RequiredFieldValidator rfv = new RequiredFieldValidator(); 
        rfv.ControlToValidate = "textbox1"
        rfv.ID = "RequiredFieldValidator1"
        rfv.ErrorMessage = "Textbox cannot be blank"
        this.form1.Controls.Add(rfv); 

    JavaScript:
     
    <script type="text/javascript"
    function KeyPress(sender, args) 
      if(args.get_keyCode() == 45) 
      { 
        alert("Enter non-negative numbers only"); 
        args.set_cancel(true); 
      } 
    function OnError(sender, args) 
       alert(args.get_reason()); 
    </script> 

    -Shinu.
Back to Top