使用 getElementById 方法进行 JavaScript 电子邮件验证
对于电子邮件验证,我有这两个输入字段:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您缺少一些大括号和一些
.value
:You're missing some braces, and some
.value
:您的脚本中缺少一些
.value
属性。这对你有用吗?You are missing some
.value
attributes in your script. Does this work for you?