0

I'm trying to validate what a user enters into a texbox on the client-side, using javascript. I have also added a span close to my input tag like so,

<div>
                        <label for="fname">First Name*</label>                      
                        <input id="fname" name="fname" maxlength="30" type="text" /><span style="display:none;" id="fnameerror" name="fnameerror">*Please enter your firstname</span>
    </div>

Here's the javascript code snippet validating the input,

if(document.getElementById('fname').value.length==0){
            msg='Please enter your first name';
            document.getElementById('fnameerror').style.display='inline';
            document.getElementById('fnameerror').style.color='red';
            valid=false;
        }

What I want to achieve now is, 1) The textbox with the error should gain focus. 2) After the error message is displayed and the user enters a valid data, the error message should disappear.

How do I achieve this. I'm fairly new to javascript. Thanks.

3 Answers 3

2

Change your JS code:

document.getElementById('fname').onkeyup = function() {
if(document.getElementById('fname').value.length==0){
            msg='Please enter your first name';
            document.getElementById('fnameerror').style.display='inline';
            document.getElementById('fnameerror').style.color='red';
            valid=false;
            document.getElementById('fname').focus();
        } else {
            valid=true;
            document.getElementById('fnameerror').style.display='none';
        }
}

Fiddle.

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

11 Comments

Thanks @chris970ng! this works without the document.getElementById('fname').onkeyup = function() part. Please can you explain what that part is doing so i can know how to add it to my code. Thanks for your quick response.
So basically the document.getElementById('fname').onkeyup will get the following JS code to run once anything is in the input text is modified. Then document.getElementById('fname').focus(); will get the input text to focus once the error is displayed. The else statement and whatever script that follows will get the element with ID fnameerror to hide. Hope my explanation helped.
okay, I tried adding that to my code, on clicking the submit button i didn't get any error for the input field but when i entered text and removed the text the error comes up. What is wrong please? @chris97ong
Set the ID of the submit button to submitBtn. JS: document.getElementById('submitBtn').onclick = function() { if(document.getElementById('fname').value.length==0){ msg='Please enter your first name'; document.getElementById('fnameerror').style.display='inline'; document.getElementById('fnameerror').style.color='red'; valid=false; document.getElementById('fname').focus(); } else { valid=true;document.getElementById('fnameerror').style.display='none'; } } Fiddle
Sry had to squeeze everything into one comment
|
1

If you've read about HTML5, it allows you to add form validation as attribute fields directly instead of having to write code for it. It also presents things neatly. Have a look. This might help: http://diveintohtml5.info/forms.html

Comments

0

I will suggest to use Jquery validator. Of course you need to include jquery,and jquery plugin, but you do not need time to write validation from the scratch only to implement what exist.

Comments

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.