简体   繁体   English

在ASP.NET中的if语句中使用正则表达式验证用户输入

[英]Validating User Input Using Regex within if Statement in asp.NET

I'm a complete beginner with C# and the .net framework, but have some experience with Python & scripting in a software-specific language I use at work. 我是C#和.net框架的完整入门者,但是对Python和使用我在工作中使用的软件特定语言的脚本有一定的经验。 For my first ASP.NET project, I've taken a form that my director built and added validation so any input other than "420" will not pass front end, and automatically display a popup to try again before going on to the next field. 对于我的第一个ASP.NET项目,我采用了我的主管构建并添加了验证的形式,因此除“ 420”以外的任何输入都不会通过前端,并自动显示一个弹出窗口,然后再尝试下一个字段。

I would like to modify my working script to check the user input variable against a RegularExpression, and invalidate responses that are not Valid Visas, or Valid Mastercards, but have not had success in testing. 我想修改我的工作脚本,以根据RegularExpression检查用户输入变量,并使不是有效签证或有效万事达卡但未成功进行测试的响应无效。

I would appreciate any guidance possible, and any suggestions on a good book or printable archive of LearningMaterial/tutorials on developing with asp.net and C# for beginners. 我将不胜感激,并希望获得关于一本好书或可打印的LearningMaterial / tutorials的书籍或有关使用asp.net和C#进行初学者开发的教程的建议。 As you can tell, I'm not there with the Syntax yet lol. 如您所知,语法尚不完善。

ORIGINAL CODE 原始码

 <label>Credit Card Number<input type="text" size="24" name="rmwebsvc_pudf_CCNumber" class="required" style="width:300px;" onblur="cc_number_saved = this.value;
  this.value = this.value.replace(/[^\d]/g, '');
  if(this.value != ^(?:4[0-9]{12}(?:[0-9]{3})?          # Visa
 |  (?:5[1-5][0-9]{2} ) {
    alert('Sorry, that is not a valid Credit Card number - please try again!');
    this.value = '';
  }
" onfocus="if(this.value != cc_number_saved) this.value = cc_number_saved;" /></label>

I recommend you to use fluent validation. 我建议您使用流利的验证。 It has a good integration with asp. 它与ASP集成良好。 Net. 净。 Here is the link https://fluentvalidation.net After doing that you don't need to call the validation explicitly because it should be integrated into the MVC pipeline, which means that you get an invalid ModelState in case the validate finds an error. 这是链接https://fluentvalidation.net之后,您无需显式调用验证,因为它应该集成到MVC管道中,这意味着在验证发现错误的情况下,您将获得无效的ModelState。

They have built-in validators, but you can write your own. 它们具有内置的验证器,但是您可以编写自己的验证器。 One of the built-in is a regular expression. 内置函数之一是正则表达式。 Check it here https://fluentvalidation.net/built-in-validators . 在此处检查https://fluentvalidation.net/built-in-validators The credit card is another built-in they have, if it is not implemented at this moment I think they are working on it now. 信用卡是他们拥有的另一种内置功能,如果目前尚未实现,我认为他们现在正在使用它。 Check here. 在这里检查。 https://github.com/JeremySkinner/FluentValidation/blob/master/src/FluentValidation/Validators/CreditCardValidator.cs Sure there are other ways to validate the input, but also with this one, you can isolate your validation logic and reuse it in another project that has the same business rules or in another UI(web, desktop) that shares the same logic. https://github.com/JeremySkinner/FluentValidation/blob/master/src/FluentValidation/Validators/CreditCardValidator.cs当然,还有其他方法可以验证输入,但是通过这种方法,您可以隔离验证逻辑并重用它在具有相同业务规则的另一个项目中,或在具有相同逻辑的另一个UI(网络,桌面)中。 Where it shines is on the testing. 它的亮点在于测试。 Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM