电子邮件字段验证功能 [英] Email Field Validation function

查看:121
本文介绍了电子邮件字段验证功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能



  function  validateEmail() {
var emailID = document .form1.txtUEmail.value;
atpos = emailID.indexOf( @);
dotpos = emailID.lastIndexOf( );
if (atpos< 1 ||(dotpos - atpos< 2 )){
alert( 请输入正确的电子邮件ID
document .form1.txtUEmail.focus();
return false ;
}
return true );
}



和文本字段是

 <   asp:TextBox     ID   =  txtUEmail   < span class =code-attribute> runat   =  server    CssClass   = 输入 >  <   / asp:TextBox  >  





和通话参数是

 btnSave.Attributes.Add(  onclick  javascript:validateEmail()); 



但我确实不工作意味着不生成alrt并保存记录是一个输入它。需要帮助

解决方案

我之前尝试使用jQuery Attr函数添加事件,它似乎不起作用。



我认为原因必须是客户必须以特定方式添加事件或事件是函数丢失。



尝试:

 btnSave.onclick = validateEmail; 





 btnSave.onclick =  function (){
validateEmail();
}







如果这不起作用,请告诉我,我会再看看^ _ ^



Andy



更新:



通过Jquery这更容易:



< script src =   https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.js>< /脚本> 
< script type = text / javascript>



; 
function validateEmail(){
var emailTxt =


input [id ='txtUEmail']
< span class =code-keyword> var emailID =


(emailTxt).val();
atpos = emailID.indexOf( @);
dotpos = emailID.lastIndexOf( );
if (atpos< 1 ||(dotpos - atpos< 2 )){
alert( 请输入正确的电子邮件ID

I have the following function

function validateEmail() {
        var emailID = document.form1.txtUEmail.value;
        atpos = emailID.indexOf("@");
        dotpos = emailID.lastIndexOf(".");
        if (atpos < 1 || (dotpos - atpos < 2)) {
            alert("Please enter correct email ID")
            document.form1.txtUEmail.focus();
            return false;
        }
        return (true);
    }


and the text field is

<asp:TextBox ID="txtUEmail" runat="server" CssClass="input"></asp:TextBox>



and calling statment is

btnSave.Attributes.Add("onclick", "javascript:validateEmail()");


But i does not working means does not generate the alrt and save the record is a enter it. Help is needed

解决方案

I have tried adding events using the jQuery Attr function before and it doesn't seem to work.

I think the reason must be that the client has to add the event in a particular way or the fact that it's a function gets lost.

Try:

btnSave.onclick = validateEmail;


or

btnSave.onclick = function(){
 validateEmail();
}




If that doesn't work, let me know and I'll look further ^_^

Andy

UPDATE:

Via Jquery this is much easier:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.js"></script>
<script type="text/javascript">


;
function validateEmail() {
        var emailTxt =


("input[id='txtUEmail']") var emailID =


(emailTxt).val(); atpos = emailID.indexOf("@"); dotpos = emailID.lastIndexOf("."); if (atpos < 1 || (dotpos - atpos < 2)) { alert("Please enter correct email ID")


这篇关于电子邮件字段验证功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆