user2654953
user2654953

Reputation: 221

Form validation using JS

I am trying to run a check on all of my text fields using Javascript. I have looked at several examples here and other places to figure out what I am missing. I just can't get out of the weeds.

function validate(form){
    var textFieldsReq = document.getElementsByName('textFieldReq');

    for(i=0,n=textFieldsReq.length;i<n;i++){
    if ( !textFieldsReq.value ){
        alert ( 'You need to fill in ' + textFieldsReq[i] + '!');
        textFieldsReq.focus();
        return false;
        }
    }
    return true;
};

Your direction would be greatly appreciated.

Upvotes: 1

Views: 59

Answers (2)

user3589620
user3589620

Reputation:

HTML does this job for you! You can use required in the <input> tag:

<input type="text" required />

Upvotes: 0

Raghuveer
Raghuveer

Reputation: 1493

Change your if condition to textFieldsReq[i]

Upvotes: 3

Related Questions