RegularExpressionAttribute fails validating right data - regex

I have a regular expression that works great when I try it:
System.Text.RegularExpressions.Regex.IsMatch("universal",#"^[A-Za-z0-9 ._’&-/s]{0,100}$")
true
System.Text.RegularExpressions.Regex.IsMatch("universal £$%$£%",#"^[A-Za-z0-9 ._’&-/s]{0,100}$")
false
But when I use it as a validation filter:
[RegularExpression(#"^[A-Za-z0-9 ._’&-/s]{0,100}$", ErrorMessage = "The parameter is not valid")]
It works in the client side, but it does not work on the server side. For example when I pass the word "universal" the ModelState contains an error regarding the field marked with that regex validator.
This attribute is the only validation rule applied to that field, what may be the problem?
Cheers.

Related

Pattern validation in angular 7

I have a contact number field in my Angular 7 form.I used 'form builder' and 'validators.pattern' for validation.In the HTML, I tried two ways to determine whether there was an error ,but both didnt work.
TypeScript:
mobnumPattern = "^[6-9][0-9]{9}$";
this.myForm = this.formbuilder.group({
contact_no: ['', [Validators.required,Validators.pattern(this.mobnumPattern)]],}
)
1.When I used below HTML, validation always shows true
*ngIf="((myForm.controls['contact_no'].touched) && (myForm.controls['contact_no'].hasError(pattern)))"
2.When I used below HTML, validation always shows false
*ngIf="((myForm.controls['contact_no'].touched) && (myForm.controls['contact_no'].errors.pattern))"
Any idea how to solve this?.
Thanks in Advance.
Lets go over both the cases you have mentioned.
1.When I used below HTML, validation always shows true
I tried recreating the issue in stackblitz but it is always false unlike what you said. Anyway the check myForm.controls['contact_no'].hasError(pattern) returns false since hasError() is expecting a string as its parameter, but pattern here is undefined.
Use this to check if the form control has pattern validation errors.
*ngIf="((myForm.controls['contact_no'].touched)&& myForm.controls['contact_no'].hasError('pattern')))"
2.When I used below HTML, validation always shows false
myForm.controls['contact_no'].errors will be null if the form control does not have any validation errors. So when checking myForm.controls['contact_no'].errors.pattern in the template will throw an error and return undefined. Use a safe navigation operator to protect against a view render failure if the myForm.controls['contact_no'].errors is null.
Like this:
*ngIf="((myForm.controls['contact_no'].touched) && (myForm.controls['contact_no'].errors?.pattern)"
I have made a stackblitz with the above mentioned fix. Check the link to see the working demo.

Custom Regex validation firing on all inputs

I'm writing a .NET MVC application and using unobtrusive validation to sanitize my client inputs based on data annotations in my model. I have an input that I do not want to allow HTML tags into and would like to display a custom error message if an html tag is entered. As such I have created a data annotation with a custom regex expression to cover these conditions, like so:
[Required(ErrorMessage = "You must provide a First Name.")]
[RegularExpression(#"<[a-z][\s\S]*>", ErrorMessage = "Invalid character")]
[DisplayName("First Name")]
public string FirstName { get; set; }
The issue with this is, no matter what character, whether it be <test> or whether it be abc will cause the Invalid Character message to appear. The required attribute works fine, and if I try a simple regex such as:
[RegularExpression("[a-z]", ErrorMessage = "Invalid character")]
This works 100% as expected, leading me to believe my regex is incorrect, nut I know it works for HTML validation as I can prove it out with online tools. What am I doing wrong?
If you take a look at the documentation of the RegularExpressionAttribute, it states:
Specifies that a data field value in ASP.NET Dynamic Data must match the specified regular expression.
So your attribute is doing the exact opposite of what you want to do is:
[RegularExpression(#"^(?!.*<.*>).*$", ErrorMessage = "Invalid character")]

Vue.js - Element UI - Form validation regexp

I'd like to validate a form using vue-js 2.3 and element-ui
Apparently, they use async-validator to validate the form. The documentation is here.
Example
https://jsfiddle.net/skd3nyqh/
Problem
Uncaught Error: Unknown rule type D*([2-9]d{2})(D*)([2-9]d{2})(D*)(d{4})D*
I do not know how to validate a field with regexp. I find the documentation not explicit enough on that topic.
From the documentation.
Pattern
The pattern rule property indicates a regular expression that the
value must match to pass validation.
Use
{required: true, pattern:/\D*([2-9]\d{2})(\D*)([2-9]\d{2})(\D*)(\d{4})\D*/, message: 'Please input phone', trigger: 'blur' }
Updated fiddle.

Laravel 4 regex email validation

I am trying to add validation, inside my User model to validation emails using regex.
However, it's spits a dummy out at the first apostrophe.
'email' => 'required|regex:/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+#[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/',
Have you tried the 'email' validation rule?
'email' => 'required|email|unique:users,email'
http://laravel.com/docs/4.2/validation#rule-email
As the answer to this question on SO states, there is no simple regular expression to validate an email-address. Using your RegEx could maybe catch valid addresses (although that's just speculation of mine). Using the email-validation-rule would be my first choice.
But you are right, this is just the server side in the first place, if you ignore redirecting users back with input and error messages..
On the client-side, you would have some options. The first one would be to simply rely on the build in browser-validation, by declaring the corresponding input-field as an email-address which you should do anyway:
{{ Form::email($name, $value = null, $attributes = array()) }}
Another, more advanced way would be to create some kind of helper to check the typed input via Ajax using the same validation rule and returning the error messages or sth. similar. This could be an additional route to your Model-Resource for example. This way, you would be stable and consistent.

Regular expression for validating url with parameters

I have been searching high and low for a solution to this, but to no avail. I am trying to prevent users from entering poorly formed URLs. Currently I have this regular expression in place:
^(http|https)\://.*$
This does a check to make sure the user is using http or https in the URL. However I need to go a step further and validate the structure of the URL.
For example this URL: http://mytest.com/?=test is clearly invalid as the parameter is not specified. All of the regular expressions that I've found on the web return valid when I use this URL.
I've been using this site to test the expressions that I've been finding.
Look I think the best solution for testing the URL as :
var url="http://mytest.com/?=test";
Make 2 steps :
1- test only URL as :
http://mytest.com/
use pattern :
var pattern1= "^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)([A-Za-z]){2,3}(\/)?";
2- split URL string by using pattern1 to get the URL query string and IF URL has Query string then make test on It again by using the following pattern :
var query=url.split(pattern1);
var q_str = query[1];
var pattern2 = "^(\?)?([0-9A-Za-z]+=[0-9A-Za-z]+(\&)?)+$";
Good Luck,
I believe the problem you are having comes from the fact that what is or is not a valid parameter from a query string is not universally defined. And specifically for your problem, the criteria for a valid query is still not well defined from your single example of what should fail.
To be precise, check this out RFC3986#3.4
Maybe you can make up a criteria for what should be an "acceptable" query string and from that you can get an answer. ;)