Check domain is valid Node.js - regex

In Node.js, how can I check if a domain issued by the user is possible and contains allowed characters only?
I do not want to check if the actual domain is existent, only that it is syntactically correct.
Eg. something.something.something should be allowed, where "*)()-.net shouldn't.
I have tried to use some of the regexs on the question How to validate domain name in PHP? however, I'm actually unsure of how to use these in node. They seemed to always come out false.

The npm package validator would be the best choice and a trustable project:
var validator = require('validator');
validator.isURL('google.com', { require_valid_protocol: false }); //=> true
Package: validator
Weekly Downloads
4,309,787 (today)

Try something like
var reg = new RegExp("[^a-z0-9-.]","i");
reg.test("asdasd.com")//returns false, invalid characters not found
reg.test("asd(.com")//returns true ,invalid characters found

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.

how to obtain a regular expression for validation email address for one domain ONLY?

I am struggling with writing a regex for validating email address for only one domain.
I have this expression
[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}
But the issue is that for example hello#gmail.com.net is valid and I only want to be only valid for only one domain. So hence I do not want hello#gmail.com.net to be valid.
Help is needed. Thank you!
try this [A-Z0-9a-z._%+-]+#[A-Za-z0-9-]+\.[A-Za-z]{2,64}.
In your regex is a dot in the allowed characters behind the #.
You can use something like:
\b[A-Z0-9a-z._%+-]+#gmail\.com\.net\b
Regex Demo
I found this regex for Swift:
[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}
It has an extra backslash.
I found it here: http://emailregex.com/
Regards,
Melle
I know you already accept an answer but this idea just cross my mind. You can use URLComponents to split the email address into user and host and validate each component separately:
func validate(emailAddress: String) -> Bool {
guard let components = URLComponents(string: "mailto://" + emailAddress),
let host = components.host else
{
return false
}
return host.components(separatedBy: ".").count == 2
}
print(validate(emailAddress: "hello#gmail.com")) // true
print(validate(emailAddress: "hello#gmail.com.net")) // false
print(validate(emailAddress: "hello")) // false
Your requirement has a big flaw in it though: valid domains can have two dots, like someone#bbc.co.uk. Getting a regex pattern to validate an email is hard. Gmail, for example, will direct all emails sent to jsmith+abc#gmail.com to the same inbox as jsmith#gmail.com. The best way is to perform some rudimentary check on the email address, then email the user and ask them to click a link to confirm the email.
You can try with below pattern.
/^(([^<>()\[\]\.,;:\s#\"]+(\.[^<>()\[\]\.,;:\s#\"]+)*)|(\".+\"))#((REPLACE_THIS_WITH_EMAIL_DOMAIN+\.)+[^<>()[\]\.,;:\s#\"]{2,})$/i;
for Eg.
/^(([^<>()\[\]\.,;:\s#\"]+(\.[^<>()\[\]\.,;:\s#\"]+)*)|(\".+\"))#((gmail+\.)+[^<>()[\]\.,;:\s#\"]{2,})$/i;

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. ;)

RegularExpressionAttribute fails validating right data

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.