开发者

ASP.NET textbox and Regular expression

iam using regular expression in javascript function to validate email. iam calling javascriptfunction using asp.net custom validator. email validation is working. when i click the textbox the cursor is in the middle of the textbox. it's not moving to end of the text or beginning of the textbox, if the textbox is empty. do you have any idea about this error.

<asp:TextBox ID="txt_piEmail"  CssClass="txtbox" runat="server" Text=""></asp:TextBox>  

<asp:CustomValidator ID="custom_valPatientInfoEmail" Display="None" ErrorMessage="Email" runat="server" ControlToValidate="txt_piEmail" SetFocusOnError="true" ClientValidationFunction="validEmail">

.txtbox {
font-size: 11px;
height:15px;
font-weight: normal;
color: #333333;
line-height: 18px;
border: 1px solid #b8c6d1;

}

 function validEmail(src, args) {



     args.IsValid = true;

     var email = document.getElementById("<%=txt_piEmail.ClientID%>");

     var 开发者_如何学PythonemailID = email.value;


     var message = "";
     var list;
     if (emailID.indexOf(';') > 0)
         list = emailID.split(';');
     else
         list = emailID.split(',');
     for (y = 0; y < list.length; y++) {
         if (trimString(list[y]) != '')
             if (echeck(trimString(list[y])) == false) {

                 args.IsValid = false;



             }
     }


     }



       function trimString(str) {
     //str = this != window? this : str;
     return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
 }

i can use regularexpression validator. i don't have regular expression for regular expression validator. i wan to validate the email for the following format: 1. giri@hotmail.com - trailing space between the email id. 2.giri@hotmail.com - without trailing space


You may want to save your self a headache and use the "regular expression validator"

you would then have something like:

<asp:TextBox ID="txt_piEmail"  CssClass="txtbox" runat="server" Text=""></asp:TextBox>  
<asp:asp:RegularExpressionValidator runat="server" id="regexVal" ControlToValidate="txt_piEmail" ValidationExpression="^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$" Display="None" ErrorMessage="Email" />

This will validate IN JAVASCRIPT and at the server (assuming you do some sort of page.validate on your event).
You can switch out the actual regular expression, i just pulled it off the first google result.
You can read more about it here:
http://msdn.microsoft.com/en-us/library/eahwtc9e.aspx


i set the email field in database as nchar. now i change it to varchar. it's working fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜