Running Javascript onFocus - Changing DIV html text
Currently I am trying to setup my contact form to give an error message when an invalid email is entered. When a blank or incorrect e-mail is submitted the page should do the following:
- Sign Up text changes to 'Retry'
- Red text appears within input field stating 'Address entered not v开发者_C百科alid'
When the user focuses on the input field, or presses 'retry'; all the form elements should go back to their original state. I have been able to get the text to change from red to black using the onFocus method. I attempted to create a small little javascript named reset, which I hoped to change the DIV text from 'Retry' back to 'Sign Up'.
I'm certain my issue is with the Javascript. Can someone point me in the right direction? :)
For code reference: http://itsmontoya.com/work/playdeadcult/indextest.php
One way of doing this is, with the 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>
And the 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;
}
};
JS Fiddle demo.
Try this instead:
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;
}
}
}
Is it just the commented out block of your reset() function what you need help with? If so, you can try the jQuery version of it since you have jQuery loaded anyway =)
The following is for your reset() function which changes text color to red:
$('#email').val('').css('color', '#FF0000').css('font-size', '18px');
The following is for your blur() function which changes text color to black. Attach this to your email input text box as well and you should be set:
$('#email').blur().css('color', '#000000');
精彩评论