Reputation: 1443
I have asked a similar question to this on here using MySQL, PHP and jQuery but I am now implementing a site in C#.NET and am using the built in ASP.NET Validation.
In my user registration, I want to validate (using ajax, not after the form submit) that the users email is unique, i.e. I want to perform a sql query to see if there is a resulting row with what the user has typed into the email field. From what I have read, there is not a good way to combine jQuery validation and the asp.net validation so I need a way to do this through ASP.NET.
I am NOT using the built in Membership framework.
Here is the part of my form dealing with email
<%--email and confirm email--%>
<tr>
<td>
<ajaxToolkit:TextBoxWatermarkExtender ID="WatermarkEmail" runat="server" TargetControlID="Email" WatermarkText="Email" WatermarkCssClass="watermarked roundedcorner" />
<asp:TextBox ID="Email" runat="server" Columns="48" CssClass="roundedcorner"></asp:TextBox>
<asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email" ErrorMessage="E-mail is required." ToolTip="E-mail is required." ValidationGroup="UserInformation"><font color="red">*</font></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ForeColor="Red" ControlToValidate="Email" ValidationGroup="UserInformation" ErrorMessage="Invalid Email Format"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
<ajaxToolkit:TextBoxWatermarkExtender ID="WatermarkConfirmEmail" runat="server" TargetControlID="ConfirmEmail" WatermarkText="Confirm Email" WatermarkCssClass="watermarked roundedcorner" />
<asp:TextBox ID="ConfirmEmail" runat="server" Columns="48" CssClass="roundedcorner"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ConfirmEmail" ErrorMessage="Confirm E-mail is required." ToolTip="Confirm E-mail is required." ValidationGroup="UserInformation"><font color="red">*</font></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regexEmailValid" runat="server" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ForeColor="red" ControlToValidate="ConfirmEmail" ValidationGroup="UserInformation" ErrorMessage="Invalid Email Format"></asp:RegularExpressionValidator>
</td>
</tr>
is there any way i can implement a <asp:CustomValidator />
into the email field that will provide the functionality I described? Or will it have to be a bit more complicated....
Upvotes: 0
Views: 1964
Reputation: 196
This validation can be done using jquery and Web Methods. Use jquery to wire the onblur event of the text box to make an AJAX call to the Web Method in the code behind file. The Web Method is a static method, which should call a Data Layer method that checks whether the email ID already exists in the database. Depending on the returned value, you can show appropriate messages to the user.
You can read more about ASP.NET Web Methods here
Upvotes: 0
Reputation: 1439
Make the EMail field in your database table unique. When you try to insert, it usually throws unique constraint error. Check for that error in you code and show the validation.
Upvotes: 1
Reputation: 48076
Just do an http POST with the email in question and whatever other registration information the user has. On the back end you can do;
SELECT
COUNT(1)
FROM MyDb.dbo.Members
where email = 'email that was passed by front end'
The result of that query should always be 1 or 0. If it's 1 you return an error message and display it to the user, if it's 0 you insert the data and return success. If it's greater than 1 then you something other than your code has inserted dupes OR the code you wrote is wrong :)
Upvotes: 1