Email validation in C# (within the page_load) - c#

I'm quite new to this:
I have created a simple form which contains a textbox and button and basically when the email address entered is correct, some results are shown below (this is using a gridview control).
What I am wanting to do is have some sort of email validation for the form - but have the validation placed within the page_load (within the button click) rather than the code behind the page itself.
I'm after a simple validation that checks an email has been entered otherwise display a popup and the email format is correct (abc#abc.com) in C#

I assume that the details will be displayed only when the buton is clicked ( form submitted) if so why not add a RegularExpression validator and map it to the text box. Then use the following regular expression to validate an email.
\b[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b
This way it will increase user experience as well. The user does not have to wait for a post back to get the error alert.
Another regular expression for email format validation here.

This has been covered here.
Problem is there is no valid regex that covers the whole RFC 5322 grammar. All common regexes (like the one stated by Shoban) are too strict - example: the end part [A-Z]{2,4} that is supposed to cover the top level domain will tag .museum emails as invalid; but there are much more complex examples, like German Umlauts (vowel mutations) that have been allowed not too recently.
Our approach is to check for a superset rather than a subset of allowed emails in validation controls (like the one integrated integrated in Visual Studio that uses \w+([-+.']\w+)#\w+([-.]\w+).\w+([-.]\w+)* ) and deepen the check back on the server (maybe even use a webservice, like this one.
BTW, here is a better regex.

Related

Does a stored password Break the ASP.NET CompareValidator?

I am trying to create a registration page for my company, and so I put down Password and "Repeat Password" textboxes on my form, along with a CompareValidator. It appears to work just fine, whenever users go into both fields, type data and click the Register/Submit button.
However, I've noticed an issue where my Firefox browser during testing saves a password that I entered. After it's saved the password, if I then go back to the page "fresh", it shows the password already filled in the Password field (though not in the "Repeat Password" field for some reason).
Anyways, if I then populate the e-mail address, I can then click the Register/Submit button with no issues at all!
The most concerning thing is, when I debug into the CodeBehind and even force a validation by triggering both the control's Validate method and the Page's Validate method manually, it still somehow PASSES the validation even though, as you can see in the QuickWatches in my screenshot below, the fields have two different values!
Have I found a bug that I should report to Microsoft, or is it possible that I did something wrong in setting this all up? All I did was plop the CompareValidator on the form, and set the "ControlToCompare" to "txtUserPass" and the "ControlToValidate" to "txtRepeatPass", and the Operation is set to "Equal".
Am I perhaps doing something wrong here, or have I found a genuine bug caused by browsers storing passwords?
As specified on the documentation, the CompareValidator does not trigger for empty values:
If the input control is empty, no validation functions are called and validation succeeds.
Use a RequiredFieldValidator control to require the user to enter data in the input control.
You need to combine the CompareValidator with a RequiredFieldValidator.

Compare multiple textbox - Make sure 1 is entered

In my Content page I have 4 different TextBox's which is used to enter phone number - Mobile, Office Phone, Office Mobile & Other. I have RegularExpressionValidator for each.
I want to make sure atleast one of the above 4 textbox's has entered a value. I didn't find any example or article on net showing this situation. One way I think is in Submit button click, before Page.IsValid call, call a function that checks that one of the control has valid value. If the function returns false, show MessageBox. Is their any other way using Validators or so to comfirm that one textbox out of 4 has a valid value.
What can be the best way to achieve this ?
Any help is highly appreciated.
Thankss
You can create a Custom Validator in ASP.NET.
This post should help you out.
asp.net required field validator for at least one textbox contains text

Orchard Custom Form DropDownLists

After a bit of playing around with Orchards' Custom Forms module, i decided i wanted to use a dropdownlist to select a particular person with their email as the value for that selected option. While i was creating the form i couldn't see anyway you could set values to your options.
See below image for example:
Don't suppose anybody has come across this before or has a suggestion?
In your case I wouldn't worry about having different text and values for the fields. It's also potentially dangerous to make the recipient email an input of the HTML form.
The Custom Form Rule Event provided with Orchard gives you no way to look at the values of the content type created by the form. So, you're probably going to have to write your own. You should be able to base this on Orchard.CustomForms.Rules.CustomFormEvents.
Armed with this you'd be able to create new rules for each possible dropdown value and set the email address in the action for each rule.

Don't post back textbox

I have a fairly complex form (user control actually) with one textbox control on it that needs to NOT post back. Is there a way to remove a control from the post? Yes, this textbox is editable.
More info: This is for a credit card processing form, so the "final" submit will post to another site's page. However, prior to this there is plenty of server-side processing that goes on. I know that I can move the the credit card number text box to another page - but this requirement came very late and I'll trying to not have to re-work a lot of things.
The easiest way would be to use an html input as opposed to an ASP TextBox. These are not accessible from code if runat="server" is not set on them.
Or use the viewstate property (http://msdn.microsoft.com/en-us/library/system.web.ui.control.enableviewstate.aspx)
So the situation is that you have a form that is rendered in the user's browser with an action pointing to a different site and you need to make sure that one of the form fields will not be sent when the form is submitted.
Sounds to me like you cannot in that case make absolutely sure that the value is not posted. There are many different possible ways to solve this using javascript (disable input, clear value, etc before submit) but if scripting is turned off I think you're out of luck.
But since you can prepare for sending the form to the other server (change action on form or enable button with PostBackUrl), I guess you could also then set the Enabled property on the textbox to false. That would mean that it can no longer be edited on the final page beforr posting to the other server. Or you could hide the textbox a (so it's not renered at all) and show the field as a label or literal instead.
But even then you still have to somehow make sure the secret value is not included in the viewstate of the form. Which it will be in case you use a label or literal. And also for a textbox that was disabled or hidden on the last postback. Normally the viewstate is just a base64 encoded string so it would be trivial to find the credit card number from there. You could probably fix this by turning off viewstate for the control in question (or even for the whole page) in the last post back to your page before setting the form up for posting to the other server.
If you cannot tell for sure which will be the last postback to your server, then I think you're out of luck without more significant changes. Sorry to be a downer. Some seemingly trivial things are just hard with Asp.Net web forms.
Maybe you could add a separate page that you populate with just the data that you need to send to the other server and have that a sort of "Confirmation page". In that page you could turn off viewstate, show all the data summarized (using labels and literals etc) and the actual data to post could be included in the form as hidden fields. Then that form would post to the other server when the user "Confirms".

Developing a custom-validation in asp.net for specific control and criteria

There is another relevant question asked Validation Check in asp.net
In the same scenario we need a custom validator control which will alert user for any wrong entry. This will work like this :
Developer will pass the control-name, input-value and format-required
For instance like for textbox it can be: txtName,txtName.Text, allow-alphabets-only
The accordingly format if the user input is invalid he/she will be got prompt.
Please suggest the right way to do the smae.
Thanks in advance.
You've described the default behaviour of all asp.net validators (pass control to validate etc).
What you want to achive (eg, alpha charaters only, or alphanumeric only etc) can be achieved mostly with the RegularExpressionValidator.

Categories

Resources