电子邮件ID验证 [英] validation for Email Ids

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

问题描述




我有两个文本框用于发送电子邮件ID

当我给出的价值如



in First Email:abc@abc.com

in Second Email:ABC@abc.com





它应该比较两个值并且应该显示验证两个电子邮件ID不应该相同



我怎样才能实现这个目标?

解决方案

使用comparevalidator并将运算符属性设置为 notequal

< ; asp:textbox id =   TextBox1 runat =    server >  < /   asp:textbox  >  
< asp:textbox id = TextBox2 runat = server textmode = 密码 > < / asp:textbox >
< asp:comparevalidator id = CompareValidator1 controltocompare = TextBox1 controltovalidate = TextBox2 Operator = NotEqual runat = 服务器
ErrorMessage = CompareValidator > &l t; / asp:comparevalidator >


 <   script    类型  =  text / javascript    language   =   JavaScript >  
<! -
// -------------- ------------------
//此代码比较表单中的两个字段并提交
//如果它们相同或不相同如果他们不同
// --------------------------------
function checkEmail(theForm){
if(theForm.EMAIL_1.value == theForm.EMAIL_2.value)
{
alert('两封电子邮件相同,请输入另一个电子邮件地址!');
返回false;
}其他{
返回true;
}
}
//
- >

< / script >




< 表格 action = ../\" onsubmit = return checkEmail(this); >
< p > 输入您的电子邮件地址:< br >
< input type = TEXT name = EMAIL_1 size = 20 maxlength = 20 >
< br >
请确认您的电子邮件地址:
< ; br >
< input 类型 = TEXT 名称 = EMAIL_2 size = 20 maxlength = 20 >
< br >
< input t ype = 提交 value = 发送地址! > < / p >
< / form >


解决方案



很容易。通过使用 JavaScript 转换为小写进行比较。

  function  validateEmails() {
var firstEmail = document .getElementById( ' txtFirstEmail');
var secondEmail = document .getElementById(' txtSecondEmail');

if (firstEmail!= null && secondEmail!= < span class =code-keyword> null ){
if (firstEmail.value.toLowerCase()== secondEmail.value.toLowerCase ()){
alert( 两个电子邮件ID不应相同。);
return false ;
}
}

返回 true ;
}



注意:它不会验证是否为空白。如果你想这样做,那么请自己添加这些代码。我刚刚给出了根据您的要求验证价值的代码。

演示



[演示]忽略案例的电子邮件验证 [ ^ ]

Hi
I have two text boxes for email ids
when I give values like

in First Email : abc@abc.com
in Second Email : ABC@abc.com


It should compare both values and should display validation "both email ids should not be same"

How can I achieve this?

解决方案

use comparevalidator and set the operator property as notequal

<asp:textbox id="TextBox1" runat="server"></asp:textbox>
<asp:textbox id="TextBox2" runat="server" textmode="Password" ></asp:textbox>
<asp:comparevalidator id="CompareValidator1" controltocompare="TextBox1" controltovalidate="TextBox2"  Operator="NotEqual" runat="server" 
                                ErrorMessage="CompareValidator"></asp:comparevalidator>


<script type="text/javascript" language="JavaScript">
<!--
//--------------------------------
// This code compares two fields in a form and submit it
// if they're the same, or not if they're different.
//--------------------------------
function checkEmail(theForm) {
    if (theForm.EMAIL_1.value == theForm.EMAIL_2.value)
    {
        alert('Both Emails are same,please enter another Email address!');
        return false;
    } else {
        return true;
    }
}
//-->
</script>




<form action="../" onsubmit="return checkEmail(this);">
<p> Enter Your Email Address:<br>
<input type="TEXT" name="EMAIL_1" size="20" maxlength="20">
<br>
Please Confirm Your Email Address:
<br>
<input type="TEXT" name="EMAIL_2" size="20" maxlength="20">
<br>
<input type="SUBMIT" value="Send Address!"></p>
</form>


Solution


Very easy. Compare by converting to lower case using JavaScript.

function validateEmails() {
    var firstEmail = document.getElementById('txtFirstEmail');
    var secondEmail = document.getElementById('txtSecondEmail');

    if (firstEmail != null && secondEmail != null) {
        if (firstEmail.value.toLowerCase() == secondEmail.value.toLowerCase()) {
        alert("Both email ids should not be same.");
            return false;
        }
    }

    return true;
}


Note: It does not validate for blank. If you want to do that, then please add those code yourself. I have just given the code for validating the value according to your requirements.

Demo


[Demo] Email Validation ignoring the case[^]


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

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