html structure:
<form method="post" id="comment-form" accept-charset="UTF-8">
<div class="form-item form-type-textfield form-item-name">
<input type="text" id="edit-name" name="name" value="" size="30" maxlength="60" class="form-text required" />
</div>
<div class="form-item form-type-textfield form-item-mail">
<input type="text" id="edit-mail" name="mail" value="" size="30" maxlength="64" class="form-text required" />
</div>
<div class="form-item form-item-homepage">
<input type="text" id="edit-homepage" name="homepage" value="" size="30" maxlength="255" class="form-text" />
</div>
<div id="edit-comment-body">
<label for="edit-comment-body-und-0-value">comment <span title="required" class="form-required">*</span></label>
<textarea class="text-full form-textarea required" id="edit-comment-body-und-0-value" name="comment_body[und][0][value]" cols="60" rows="5"></textarea></div>
</div>
jquery code:
$(document).ready(function(){
if($("#comment-form #edit-name").val()=='' || $("#comments #edit-email").val() =='' ||$("edit-comment-body"))
$("#comments #edit-submit").click(function(){
$('#comment-form #edit-name').append('<span style="color:red;">please type your name</p>');
$('#comment-form #edit-mail').append('<span style="color:red;">please type your emali</p>');
$('#comment-form #edit-comment-body label span').append('<span style="color:red;">pleae type the content</p>');
});
});
i want to add some red text after the input text. but the above code can’t work?what’s wrong with my jquery code? thank you
There are quite a few things wrong – you use of selectors first of all is bad. You should stick to single id’s where possible, e.g.:
Next problem is, you are wasting an IF statement – when the page first loads, you are checking if some inputs are blank – which they are likely to be, and still, even if they wern’t you don’t want someone to be able to empty and it pass as valid simply because the function which listens to the
.click()wasn’t added.After the page loaded and the fields are blank, then you add a
.click()listener to a button. It will be easier to add the.click()function to the button then check the validation always.So for example you could change your jQuery to somehting like this:
You will also need to add a submit button, but I will assume you missed that off a copy paste of you HTML:
To explain the jQuery a little:
To enhance your validation, you only want to error message once, and if the mistake has been corrected, but another still exists the corrected message should be removed (imagine multiple clicks on the submit).
To do this, add a class to your error message:
And at the top of your function:
So the final
.click()function:See it working here
I will try to explain the
e.preventDefault()part.To start, the IF statement….basically an IF statement says:
So at the top of the
.click()function we declare a variable calledblankand set it to FALSE. So if no where in the code that follows that, up untill we get to the IF statement changes that to TRUE the code would not run.So in the code, each time we check if one of the inputs is blank, we add a message and change
blankto TRUE, so now the code in that IF statement will run.On to the code, the
e.preventDefault()this is a way to stop the browser doing what it would normally do by default.So when we declare a callback function, in this case the
.click()event, we are asking the jquery to listen out for clicks on something, so when it gets one, the event is also available within the code.So imagine, since the begining of the web if someone clicks on a hyperlink (
<a href=".....) the browser knows it needs to follow that link, regardless of what the jquery is doing, similarly if you submit a form, the browser knows it should follow the link declared in the declaration of the form.Now because we don’t want to submit the form IF there was a blank, we need to
preventDefault()(prevent the browser doing its default action).To do this, when we declare the
.click(function(e) {, we have declared a new variableewhich is available within the function,eis the event which has caused the.click()function to be triggered – a click on a hyperlink, a click on a submit, a click on a picture even.We could change things to slighlty longer code to make it easier to follow, for example:
etoevent(infact anything you want):