I have a user input to supply website address, obviously most users have no idea what is well formatted url so I look for a website address Regex that will follow this rules:
1) www.someaddress.com - True
2) someaddress.com - True
3) http://someaddress.com - True
4) https://someaddress.com - True
5) https://www.someaddress.co.il - True
6) http://www.someaddress.com - True
I use this Regex:
[RegularExpression(#"^((http|ftp|https|www)://)?([\w+?\.\w+])+([a-zA-Z0-9\~\!\#\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?$", ErrorMessage = "Not a valid website address")]
public string SiteUrl { get; set; }
But it's useless because it allows almost every string to pass.
Please supply a data annotation answer and not answers such as:
Uri.IsWellFormedUriString
Because .net doesn't support client side validation for custom attributes.
There is a UrlAttribute to validate URLs, but it does enforce the protocol being there, which it appears you don't want.
However, the source code is available and it does use a regular expression that you can steal and modify. Modifying just the protocol portion to be optional the way you want, you get this:
^((http|ftp|https)://)?(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*#)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|#)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|#)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|#)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|#)|\/|\?)*)?$
(Side note: I noticed that your regex allowed www://, which is suspicious. I took it out in this, but if you truly do need that, then you can add it.)
These are values I tested with:
www.someaddress.com Yes
someaddress.com Yes
http://someaddress.com Yes
https://someaddress.com Yes
https://www.someaddress.co.il Yes
cow No
hi hello.com No
this/that.com No
In the comments of the source code it does say:
This attribute provides server-side url validation equivalent to jquery validate, and therefore shares the same regular expression. See unit tests for examples.
Related
EmailAddressAttribute in .NET 4.6.1 allow dot on the end.
That means that following email: someone#google.com. is valid.
For Microsoft this email is valid.
But, for example, for PayPal, email is not valid.
So does anybody know, is dot on the end of email valid or not?
There is a lot of contending information about whether this is legal, or valid. Those are two different views, and I'm going to try and explain a bit why.
Email addresses are described in part by RFC 5322 - Internet Message Format which explains email formats in excruciating detail.
In section 3.4.1 - Addr-spec, the email address format is explained. I'm paraphrasing for brevity but the general format is:
local-part#domain
With local-name described as one of the following dot-atom / quoted-string / obs-local-part and domain described as dot-atom / domain-literal / obs-domain.
So it's a domain name, which is described in RFC 1034 - Domain Names - Concepts And Facilities.
A domain name can be ambiguous or unambiguous, which is defined by the absence or presence of the trailing dot. Ambiguous domain names are not guaranteed to resolve to a location, but most (if not all at this point) DNS search lists append a period behind the scenes if one is not present, but this is a Quality-of-Life improvement. Unambiguous domain names must contain a trailing period, it's basically a terminating character in DNS.
Thomas Flinkow already mentioned what the source looks like, I just wanted to give some context as to why - historically - the regex might be the way it is. A trailing period is legal, but validity is defined by the mail providers.
Well, since I did not find any documentation on that, I checked the source of the EmailAddressAttribute to see if any comments explained whether or not
someone#google.com.
is considered valid, but I did not find comments regarding that.
What I did find is this regular expression, which is used to determine whether or not an address is invalid:
^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|
[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|
[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*
(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|
[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09
\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*
(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([a-z]|\d|
[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|
[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|
[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|
[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|
[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|
[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|
[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|
[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$
which is obviously quite long. The interesting part however is this little part at the very end:
\.?
which means match between 0 and 1 "." characters.
Therefore I feel like it is intentionally built in that email addresses ending with a period are considered valid, although I have not found any external resources on whether email addresses ending with a period are actually allowed by any email providers.
For the validating, I would advise to not rely only on the EmailAddressAttribute, but to make your own validator (since EmailAddressAttribute is sealed and you can not derive your own attribute), which could look somewhat like this:
public bool IsValidEmailAddress(string email)
{
var emailValidator = new EmailAddressAttribute();
return emailValidator.IsValid(email) && !String.EndsWith(".");
}
In the code above, the attribute is used to provide the basic checking implementation, and !String.EndsWith(".") takes care of email addresses falsely determined as valid that have a trailing period.
TL;DR: The definite answer seems to be what Yannick Meeus has written:
A trailing period is legal, but validity is defined by the mail
providers.
and therefore Microsoft seems to have conformed to the rules, even though in practice only few (none?) mail providers allow a trailing period. So you have to decide whether or not you also confirm to the formal rules and allow the trailing "." or if you want to exclude it (as demonstrated in the sample code above).
How to check what is wrong in domain URL in C#
I want to updated domain URL when invalid domain enter.
Input Put of Domain: OutPut Like
1)http:/localhost:1234/------>http://localhost:1234/
2)http://localhost:1234------>http://localhost:1234/
3)http:localhost:1234/------->http://localhost:1234/
4)http:localhost:1234-------->http://localhost:1234/
5)localhost:1234/------------>http://localhost:1234/
6)localhost:1234------------->http://localhost:1234/
Also above all test cases with HTTPS
May be need add more test cases.
I have code of nopCommerece for warning but it's use only current store .
How I develop a code for enter domain is valid or not and return valid domain.
My understanding of the question is you want to take in a given URL and output a correction. At the very minumum you are looking for the string "localhost:1234". You could use a regular expression to check for the existence of this string. If true, output "http://localhost:1234/"
The regular express is "/localhost:1234/g" and can be found here: http://regexr.com/3e2n8
To check this regular expression in C# you will code:
Regex regex = new Regex(#"/localhost:1234/g");
Match match = regex.Match("http:/localhost:1234/"); // your given string
if (match.Success)
{
// your given string contains localhost:1234
}
In any domain name the following are important:
www..com:
portnumber is 80 by default
but still, to check and get the Exception, use this URL,
Best way to determine if a domain name would be a valid in a "hosts" file?
This is an odd (to me anyway) query string problem.
I'm using a installation tool that has web serial number validation. Basically the install passes a users email and serial number to a web page (or a controller method for MVC) and that takes the query string arguments and does magic to validate the installation.
One of the arguments is the email address passed in the query string.
I recently has a user who used ‘+’ email addressing to purchase a subscription. All worked well until he went to install the product and had to get past the validation screen.
After doing some digging I found that instead of receiving
‘joe+foo#gmail.com’
The validation code receives
‘joe foo#gmail.com’
Of course the space ruins the validation attempt as his email address is now wrong.
I've spoken with the install tool company (Advanced Installer, best install tool on the planet) and they claim (and I believe them) that the email is sent correctly.
So that leaves me at how do I get the asp.net mvc querystring parser do to the right thing for that particular argument and pass the string with the '+' to the contoller method ?
It's asp.net mvc 5 if it matters.
Thanks for any help you can spare.
UPDATE
The problem is with the sending, not the reception. the plus sign ends up unencoded so it translate to a space= when it get handled by the query string parser.
So what I am looking for is a way to customize the query string parser for that particular URL (and only that URL).
The shortcut to a fix is to replace spaces with a plus sign in the email arg. Simple, but I hate that kind of hackery in my code I;d prefer to have it use a customized parser to that if I need it else where I can just plug it in any way it goes.
You can customize just about everything else in asp.net mvc so I was wondering if there was a way to do the query string pasring in some custom fashion.
Assuming you are calling the URL from javascript, instead of doing this:
url += "?email=" + email;
Encode the value like this:
url += "?email=" + encodeURIComponent(email);
If you are calling the URL from the server, then:
string encodedEmail = Server.UrlEncode(email);
UPDATE
If you can't change where the URL is getting called, then you don't have any other option than:
HttpUtility.UrlEncode(Request.QueryString["email"]);
or:
email = email.Replace(' ', '+');
It looks like I'm going to have to go with my hack solution of swapping space for a plus sign in that particular query string parameter. Not the ideal solution in my way of thinking, but it will do the trick.
I am building a system where the user builds a query by selecting his operands from a combobox(names of operands are then put between $ sign).
eg. $TotalPresent$+56
eg. $Total$*100
eg 100*($TotalRegistered$-$NumberPresent$)
Things like that,
However since the user is allowed to enter brackets and the +,-,* and /.
Thus he can also make mistakes like
eg. $Total$+1a
eg. 78iu+$NumberPresent$
ETC...
I need a way to validate the query built by the user.
How can I do that ?
A regex will never be able to properly validate a query like that. Either your validation would be incomplete, or you would reject valid input.
As you're building a query, you must already have a way parse and execute it. Why not use your parsing code to validate the user input? If you want to have client-side validation you could use an ajax call to the server.
I need a way to validate the query built by the user.
Personally, I don't think it is a good idea to use regex here. It can be possible with help of some extensions (see here, for example), but original Kleene expressions aren't fit for checking whether unlimited number of parentheses is balanced. Even worse, too difficult expression may result in significant time and memory spent, opening doors to denial-of-service attacks (if your service is public).
You can make use of a weak expression, though: one which is easy to write and match with and forbids most obvious mistakes. Some inputs will still be illegal, but you will discover that on parsing, as Menno van den Heuvel offered. Something like this should do:
^(?:[-]?\(*)?(?:\$[A-Za-z][A-Za-z0-9_]*\$|\d+)(?:\)*[+/*-]\(*(?:\$[A-Za-z][A-Za-z0-9_]*\$|\d+))*(?:\)*)$
Hey guys I managed to get to my ends(Thanks to Anirudh(Validating a String using regex))
I am posting my answer as it may help further visitors.
string UserFedData = ttextBox1.Text.Trim().ToString();
//this is a regex to detect conflicting user built queries.
var troublePattern = new Regex(#"^(\(?\d+\)?|\(?[$][^$]+[$]\)?)([+*/-](\(?\d+\)?|\(?[$][^$]+[$]\)?))*$");
//var troublePattern = new Regex(#"var troublePattern = new Regex(#"^(?:[-]?\(*)?(?:\$[A-Za-z][A-Za-z0-9_]*\$|\d+)(?:\)*[+/*-]\(*(?:\$[A-Za-z][A-Za-z0-9_]*\$|\d+))*(?:\)*)$");
string TroublePattern = troublePattern.ToString();
//readyToGo is the boolean that indicates if further processing of data is safe or not
bool readyToGo = Regex.IsMatch(UserFedData, TroublePattern, RegexOptions.None);
How would I check on registration that a user types in a specific email address? For example, i want my registration form to only allow these email addresses:
#gmail.com
#yahoo.com
#live.com
Like this:
[RegularExpression( #"#(gmail|yahoo|live)\.com$", ErrorMessage = "Invalid domain in email address. The domain must be gmail.com, yahoo.com or live.com")]
public string EmailAddress { get ; set ; }
You don't even need a regular expression; you can just use the split() function to obtain the part of the email address after the "#" and check it against your list of allowed providers.
This by itself doesn't guarantee that it's a well-formed email address (that may require a regex, and a somewhat complicated one), but it will make sure that the address ends with one of the domains on your list.
You could use a RegularExpressionValidator control and an expression to look for the email domains. You can find a sample at http://www.regexplib.com if you don't already have one.
You're probably going to want a CustomValidator as well that performs an identical server-side check. Users can circumvent your RegularExpressionValidator if they disable Javascript.
There is not built in but you can use [RegularExpression].You can write custom EmailAttribute deriving from RegularExpressionAttribute.
A very well implementation is done here
You can use following regular expression to check email:
^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)#[a-z0-9]+(\.[a-z0-9]+)\.([a-z]{2,4})$
Beside this, you can have Data Annotation Extension which has [Email] attribute that allows for validating an email address.