This is a migrated thread and some comments may be shown as answers.

Validator for RadNumericTextBox created dynamically

1 Answer 138 Views
Input
This is a migrated thread and some comments may be shown as answers.
iomega 55
Top achievements
Rank 1
iomega 55 asked on 23 Aug 2009, 09:55 PM
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.

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 24 Aug 2009, 09:45 AM
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.
Tags
Input
Asked by
iomega 55
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or