使用 getElementById 方法进行 JavaScript 电子邮件验证

发布于 2024-10-26 15:00:00 字数 2282 浏览 6 评论 0原文

对于电子邮件验证,我有这两个输入字段:

<input class="inputbox" type="text" name="data[subscriber][email]" id="field_email" size="40" value="'.$this->escape(@$this->subscriber->email).'"/>

<input class="inputbox" type="text" name="data[subscriber][retype_email]" id="field_retype_email" size="40" value="" onblur="confirm_email()" />

输入字段的名称不同( input1_name, input2_name ),并且我有工作脚本:

<script type = "text/javascript">
function confirm_email() {
email = document.adminForm.input1_name.value;
re_email = document.adminForm.input2_name.value;
if ( email != re_email ) {
alert ("ERROR: El correo electronico no coincide. Escribe tu correo electronico nuevamente")
document.adminForm.input1_name.value = "";
document.adminForm.input2_name.value = "";
document.adminForm.input1_name.focus();
}
else {}
}
</script>

但我需要通过 id 访问元素,因为输入元素需要用括号格式化(这在 JavaScript 中不起作用)。因此,我导出了以下脚本:

<script type="text/javascript">
function confirm_email(){
    var email = document.getElementById('field_email');
    var re_email = document.getElementById('field_retype_email');
    if( email != re_email )
        alert("ERROR: El correo electronico no coincide. Escribe tu correo electronico nuevamente")
        document.getElementById('field_email').value = "";
        document.getElementById('field_retype_email') = "";
        document.getElementById('field_email').focus();
    else {} 
}
</script>

它不起作用

如果我只使用脚本的这一部分,它就会起作用:

<script type="text/javascript">
    function confirm_email(){
        var email = document.getElementById('field_email');
        var re_email = document.getElementById('field_retype_email');
        if( email != re_email )
            alert("ERROR: El correo electronico no coincide. Escribe tu correo electronico nuevamente")
            else {} 
    }
    </script>

这意味着这些行是错误的:

        document.getElementById('field_email').value = "";
        document.getElementById('field_retype_email') = "";
        document.getElementById('field_email').focus();

但是,我希望重置输入元素,并且第一个输入元素是焦点(获得我使用第一个有效代码片段时获得的效果)。

如何使用 getElementById 方法执行此操作?

谢谢

for email validation I have these two input fields:

<input class="inputbox" type="text" name="data[subscriber][email]" id="field_email" size="40" value="'.$this->escape(@$this->subscriber->email).'"/>

<input class="inputbox" type="text" name="data[subscriber][retype_email]" id="field_retype_email" size="40" value="" onblur="confirm_email()" />

the name of the input fields was different ( input1_name, input2_name ), and I had working script:

<script type = "text/javascript">
function confirm_email() {
email = document.adminForm.input1_name.value;
re_email = document.adminForm.input2_name.value;
if ( email != re_email ) {
alert ("ERROR: El correo electronico no coincide. Escribe tu correo electronico nuevamente")
document.adminForm.input1_name.value = "";
document.adminForm.input2_name.value = "";
document.adminForm.input1_name.focus();
}
else {}
}
</script>

BUT I NEED to access the element by id, because the input element needs to be formatted with brackets (this does not work in javascript). And so I derived the following script:

<script type="text/javascript">
function confirm_email(){
    var email = document.getElementById('field_email');
    var re_email = document.getElementById('field_retype_email');
    if( email != re_email )
        alert("ERROR: El correo electronico no coincide. Escribe tu correo electronico nuevamente")
        document.getElementById('field_email').value = "";
        document.getElementById('field_retype_email') = "";
        document.getElementById('field_email').focus();
    else {} 
}
</script>

IT DOESN'T WORK

If I only use this part of the script it works:

<script type="text/javascript">
    function confirm_email(){
        var email = document.getElementById('field_email');
        var re_email = document.getElementById('field_retype_email');
        if( email != re_email )
            alert("ERROR: El correo electronico no coincide. Escribe tu correo electronico nuevamente")
            else {} 
    }
    </script>

which means that these lines are wrong:

        document.getElementById('field_email').value = "";
        document.getElementById('field_retype_email') = "";
        document.getElementById('field_email').focus();

but, I want the input elements to reset, and the first input element to be on focus (get the effect that I got when I used the first snippet of code that worked).

How can I do this with the getElementById method?

Thanx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

圈圈圆圆圈圈 2024-11-02 15:00:00

您缺少一些大括号和一些 .value

<script type="text/javascript">
function confirm_email(){
    var email = document.getElementById('field_email').value;
    var re_email = document.getElementById('field_retype_email').value;
    if( email != re_email ) {    //  <-- here
        alert("ERROR: El correo electronico no coincide. Escribe tu correo electronico nuevamente")
        document.getElementById('field_email').value = "";
        document.getElementById('field_retype_email').value = "";
        document.getElementById('field_email').focus();
    }    // <-- and here
    else {} 
}
</script>

You're missing some braces, and some .value:

<script type="text/javascript">
function confirm_email(){
    var email = document.getElementById('field_email').value;
    var re_email = document.getElementById('field_retype_email').value;
    if( email != re_email ) {    //  <-- here
        alert("ERROR: El correo electronico no coincide. Escribe tu correo electronico nuevamente")
        document.getElementById('field_email').value = "";
        document.getElementById('field_retype_email').value = "";
        document.getElementById('field_email').focus();
    }    // <-- and here
    else {} 
}
</script>
小清晰的声音 2024-11-02 15:00:00

您的脚本中缺少一些 .value 属性。这对你有用吗?

<script type="text/javascript">
function confirm_email(){
    var email = document.getElementById('field_email').value;
    var re_email = document.getElementById('field_retype_email').value;
    if( email != re_email ) {
        alert("ERROR: El correo electronico no coincide. Escribe tu correo electronico nuevamente")
        document.getElementById('field_email').value = "";
        document.getElementById('field_retype_email').value = "";
        document.getElementById('field_email').focus();
    }
    else {} 
}
</script>

You are missing some .value attributes in your script. Does this work for you?

<script type="text/javascript">
function confirm_email(){
    var email = document.getElementById('field_email').value;
    var re_email = document.getElementById('field_retype_email').value;
    if( email != re_email ) {
        alert("ERROR: El correo electronico no coincide. Escribe tu correo electronico nuevamente")
        document.getElementById('field_email').value = "";
        document.getElementById('field_retype_email').value = "";
        document.getElementById('field_email').focus();
    }
    else {} 
}
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文