目前,我正在尝试设置联系表单,以便在输入无效的电子邮件时给出错误消息。提交空白或不正确的电子邮件时,页面应执行以下操作:
当用户关注输入字段或按“重试”时;所有表单元素都应该恢复到原始状态。我已经能够使用onFocus方法将文本从红色变为黑色。我试图创建一个名为reset的小javascript,我希望将DIV文本从“重试”更改回“注册”。
我确定我的问题在于Javascript。有人能指出我正确的方向吗? :)
答案 0 :(得分:0)
它只是你的reset()函数的注释掉了你需要帮助的东西吗?如果是这样,你可以尝试jQuery版本,因为你还是加载了jQuery =)
以下是您的reset()函数,它将文本颜色更改为红色:
$('#email').val('').css('color', '#FF0000').css('font-size', '18px');
以下是您的blur()函数,它将文本颜色更改为黑色。将其附加到您的电子邮件输入文本框中,您应该设置:
$('#email').blur().css('color', '#000000');
答案 1 :(得分:0)
这样做的一种方法是使用html:
<form action="" method="post">
<fieldset>
<label for="emailAddress">Sign up for the newsletter</label>
<input type="text" id="emailAddress" name="email" />
<button id="submitButton" type="submit">Submit</button>
</fieldset>
</form>
JavaScript:
var emailEntry = document.getElementById('emailAddress');
var button = document.getElementById('submitButton');
var defaultButtonText = button.innerHTML;
emailEntry.onblur = function(){
var val = this.value;
// the following IS NOT a valid test of email address validity
// it's for demo purposes ONLY
if (val.length < 5 && val.indexOf('@') == -1){
button.innerHTML = 'retry';
button.style.color = 'red';
}
else {
button.innerHTML = defaultButtonText;
button.style.color = 'black';
}
};
button.onclick = function(){
// prevent submission of the form if the email was found invalid
// and the innerHTML was therefore set to 'retry'
if (this.innerHTML == 'retry'){
return false;
}
};
答案 2 :(得分:0)
请改为尝试:
function validate() {
if($("#buttonDiv").html()== 'Retry')
{
// oops! They are trying to get back to where they were by
// re-clicking retry. Let's reset this thing
reset()
return false;
}
var emDiv = document.getElementById('email') // only look up once.
if(emDiv) {
// the space after the . should allow for 6 long
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,6})$/;
var address = emDiv.value;
// good form issue -- never use == false. use !
if(!reg.test(address)) {
emDiv.value = 'Address entered not valid';
emDiv.style.color = '#bf4343';
emDiv.style.fontSize = '12px';
$("#buttonDiv").html( 'Retry' );
return false;
}
}
}