0

I have been searching for hours trying to find a way to validate my form while the user is typing. for example, I want have a field for zip code. I want the user to see a message underneath the field that says they have gone beyond the limit of characters for this field but before they submit the form. How can this be done?

with such code:

<form method="POST" name="wrriregistration" target="_blank"><center>
<table width="100%">
<tbody>
<tr>
<td width="149"><strong>*First Name:</strong></td>
<td width="229"><input type="text" name="first_name" size="35" maxlength="100"/></td>
<td width="123"><strong style="display:none;">Middle Initial:</strong></td>
<td width="659"><input style="display:none;" type="text" name="middle_initial" size="35" maxlength="50" /></td>
</tr>
</tbody>
</table>
</form>
7
  • You will need javascript for that, it is not a php functionality. Commented Jul 21, 2016 at 18:40
  • The maxlength attribute will take care of users trying to enter too many characters. If it's validation that can be done just based on what's present on the form, you can just use simple Javascript to check the field using either they keyup, keypress or change events. If you need to validate against something on the back-end, you'll need to use AJAX. Commented Jul 21, 2016 at 18:41
  • Okay thank you. How so? Also could you explain why it is not php functionality? Commented Jul 21, 2016 at 18:42
  • 1
    for complete form validation, u may see this. formvalidator.net/#reg-form Commented Jul 21, 2016 at 18:44
  • 1
    jsfiddle.net/ty54t8r8 Commented Jul 21, 2016 at 18:49

1 Answer 1

2

Try this :

HTML

<input type="text" name="first_name">
<div id="error">
    My custom error
</div>

CSS

#error {
    display: none;
}

#error.show {
    display: block;
}

input {
    color: #000000;
}

.invalid {
    color: #FF0000;
}

JS

var input = document.querySelector('[name="first_name"]');
var error = document.getElementById('error');

input.addEventListener('keydown', function(){
    // Whatever you want
    if(this.value.length >= 10) {
        this.classList.add('invalid');
        // You can control style of your invalid input with .invalid
        error.classList.add('show'); // Display your custom error
    } else {
        this.classList.remove('invalid');
        error.classList.remove('show');
    }
});

EDIT Explanation :

var input target your first_name input

addEventListener makes a event detection. Passed with argument 'keydown', JavaScript will listen to keys pressed.

classList is an API to manipulate classes (not supported by IE).

Try it here : https://jsfiddle.net/e3oe4ykf/

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for the response, I will try this out. I have read the above comment with this same implementation.
how can I get the length of one of those fields?
I am sorry but could you further explain how to implement? I've tried it many ways and cannot get it to run. I am just trying to warn users when character are surpassed on the name field

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.