0

In Asp.Net or Web-Developemnt what is the best way to ValidateTextBox's on a page. The TextChange event (server side) happens after you leave the textbox which is too late. I was going to use a Double.TryParse on server side.

Troy Mitchel
  • 1,790
  • 11
  • 50
  • 88
  • If you want to validate client side, you need to look at a JavaScript solution like [jQuery Validation Plugin](http://bassistance.de/jquery-plugins/jquery-plugin-validation/). You still need to validate/type cast your data server side, but this will catch and alert errors to clients as they type or when they try to submit the data! – Zachary Sep 20 '11 at 17:53
  • what if they have javascript disabled? – Troy Mitchel Sep 20 '11 at 17:59
  • @Zachary - Using client-side validation only is a bad idea unless you want to open yourself up to attack. It's too easy to bypass, and any attacker worth his salt knows how to do it. You need to validate at the server as well as at the client. Client-side is nice for a better UI, but bad for actually preventing attacks. – David Sep 20 '11 at 18:11
  • http://stackoverflow.com/questions/162159/javascript-client-side-vs-server-side-validation – David Sep 20 '11 at 18:12
  • 1
    @DavidStratton I 100% agree, this is why I mentioned they will still need to validate/type cast on the server. Client side is great for showing the clients, but does nothing to mitigate attacks! – Zachary Sep 20 '11 at 18:26
  • Sorry, didn't read your whole comment. I keep skimming instead of reading properly, and saying dumb things as a result. No offence meant! Your comment was actually very good. – David Sep 20 '11 at 18:28

3 Answers3

0

The accepted best practice is to use a Validator control.

The CompareValidator can be used to verify the Data Type by changing the "Operator" property to "DataTypeCheck".

You could also use a RegularExpressionValidator for the same effect.

If you need to do a type check and ensure the value is withing a specified range, you would use a RangeValidator.

David
  • 72,686
  • 18
  • 132
  • 173
  • I have roughly 10 textbox's, can I just use one CompareValidator then? And this is validated as they type? – Troy Mitchel Sep 20 '11 at 18:02
  • No. The Validation controls all validate a single input control. If you want to validate all of them with a single function, you could do so via JavaScript on the client, BUT that's not recommended. Client-side only validatipn leaves you open to unvalidated input if an attacker sets up a page to post to your page. The validation controls use the security best practice of validating both at the client and more importantly, at the server, which is critical where security is concerned. It's just too easy to bypass javaScript. And using the controls 10 times isn't really that difficult. – David Sep 20 '11 at 18:09
0

Check Regular expression and use RegularExpressionValidator there's a control named "RegularExpressionValidator" you found this control in toolbox under validator tab. Use that control read link which i posted and to allow only numeric use ValidationExpression="\d+"

Hope my answer help you to solve your problem.

Emaad Ali
  • 1,483
  • 5
  • 19
  • 42
0

If you are using Web Forms, I suggest that you use CompareValidator. Make sure that you specify the Type attribute of the validator as Double as well as the DataTypeCheck attribute as DataTypeCheck. If you prefer to enable client side validation then assign True to EnableClientScript attribute. I hope this helps...

Zen

UncleZen
  • 289
  • 1
  • 4
  • 22