This is a migrated thread and some comments may be shown as answers.

Email Validation

3 Answers 1569 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Andy
Top achievements
Rank 1
Andy asked on 05 Jul 2011, 08:45 PM
Hi all,

I am using the asp:RegularExpressionValidator for telerik:RadTextBox to validate email address as following link
http://demos.telerik.com/aspnet-ajax/input/examples/common/validation/defaultcs.aspx

But it is wrong when user input space at the front or at the end of the email address.
Are there any ways to trim it?

Thanks.

Andy.


3 Answers, 1 is accepted

Sort by
0
Accepted
Vasil
Telerik team
answered on 06 Jul 2011, 03:10 PM
Hi Andy,

Yes, you could trim the string on OnValueChanging event of the text box just before the validation.

Here is an example:
<script type="text/javascript">
  function TrimIt(sender, args)
  {
    var value = args.get_newValue();
    var trimmed = value.replace(/^\s+|\s+$/g, '');
    args.set_newValue(trimmed);
  }
</script>
<div>
  <telerik:RadTextBox ID="Radtextbox2" runat="server">
    <ClientEvents OnValueChanging="TrimIt" />
  </telerik:RadTextBox>
  <asp:RegularExpressionValidator ID="emailValidator" runat="server" Display="Dynamic"
    ErrorMessage="Please, enter valid e-mail address." ValidationExpression="^[\w\.\-]+@[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]{1,})*(\.[a-zA-Z]{2,3}){1,2}$"
    ControlToValidate="Radtextbox2">
  </asp:RegularExpressionValidator>
  <asp:RequiredFieldValidator ID="Requiredfieldvalidator1" runat="server" Display="Dynamic"
    ControlToValidate="Radtextbox2" ErrorMessage="Please, enter an e-mail!" />
</div>

You could also see this resources for more information:
http://www.telerik.com/help/aspnet-ajax/input-client-side-onvaluechanging.html
http://developer.loftdigital.com/blog/trim-a-string-in-javascript

I hope this helps.

Greetings,
Vasil
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Andy
Top achievements
Rank 1
answered on 11 Jul 2011, 09:28 PM
That's great.
Thanks Vasil.

Andy.
0
Neil
Top achievements
Rank 1
answered on 29 Jan 2018, 05:06 PM

For anyone else looking for an email regex, note that the one posted by Vasil doesn't allow some characters that are valid before the @ symbol e.g. apostrophe

 

Better to take from Kendo UI source code? https://github.com/telerik/kendo-ui-core/blob/master/src/kendo.validator.js:

^[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])?)*$

Tags
General Discussions
Asked by
Andy
Top achievements
Rank 1
Answers by
Vasil
Telerik team
Andy
Top achievements
Rank 1
Neil
Top achievements
Rank 1
Share this question
or