开发者

One asp.net validator for several TextBoxes

开发者 https://www.devze.com 2023-03-16 22:21 出处:网络
I have 3 TextBoxes on the page. How do I setup one validator (and what\'s kind of validator?) to validate each of TextBoxes on client side?开发者_如何学C

I have 3 TextBoxes on the page. How do I setup one validator (and what's kind of validator?) to validate each of TextBoxes on client side?开发者_如何学C

First textBox must contains only 1-3 number of digits, second - infinite number of digits, third - 0-5 number of digits


It would be better to use different validators for different text boxes. Use 3 validators for each with display message as * sign.

Then use Validation Summary control to print a single message for all of them, as you want a single message to be displayed for all. Refer this link: MSDN Validation Summary

Check this too: Validation Summary to work with client-side validations


Your expressions would be along the lines of:

[0-9]{1,3}

[0-9]+

[0-9]{0,5}

Updated following further info:

You could combine the values from each text box into a comma separated value and then validate that using [0-9]{0,3},[0-9]+,[0-9]{1,5} expression.

Either use javascript to copy the value into a hidden textbox field with it's own regex validator using above expression, or just perform the validation manually.

e.g. Using jquery

 $('#hiddenCombinedField').val($('#field1').val()+','+$('#field2').val()+','+$('#field3').val());

Run this just before you call validate on the form.

See also http://speckyboy.com/2009/12/17/10-useful-jquery-form-validation-techniques-and-tutorials-2/ for some useful reference material.


You could use a CustomValidator but i would prefer using three distinct validators, one for every TextBox.

It's possible to leave the ControlToValidate-property of the CustomValidator empty. On this way you can validate multiple controls at the same time.

0

精彩评论

暂无评论...
验证码 换一张
取 消