Validating my field on the client side and try to display a warning for each one next to the field is not a problem:
<div class="formblock">
<label class="screen-reader-text">Email</label>
<input type="text" name="email" {if $smarty.session.email} value={$smarty.session.email} disabled {/if} id="email" class="txt requiredField email" placeholder="Email:"/>
</div>
<div class="formblock">
<label class="screen-reader-text">Message</label>
<textarea name="comments" id="commentsText" class="txtarea requiredField" placeholder="Message:"></textarea>
</div>
E.g to check that they are empty I just use .each and check this for every field and append a span
next to it.
$('.requiredField').each(function () {
if ($.trim($(this).val()) == '') {
var labelText = $(this).prev('label').text();
$(this).parent().append('<span class="error">You forgot to enter a ' + labelText + '.</span>');
$(this).addClass('inputError');
hasError = true;
}
});
However on the server side the way that I normally do things is perform validation for each field and then append the error message as a var ($ERRORTEXT
in that case) then do an echo in the end and display this after ajax success on the top of the form.
$ERRORTEXT .= " <li> Name has to contain more than two characters </li> ";
if ($error)
{
echo $ERRORTEXT;
}
How can I do this per field (similar to what I do on the client side) and return it to the span next to them ? I presume that instead of returning one var back with echo I would have to return maybe a json result and then handle this ?