0

i have the following code that validate against empty textboxes. If there are empty ones, the form will not be able to submit successfully.

However,I cannot seem to get the alert to popup after clicking submit looping through the elements to find if there are any empty boxes to fill in.

any advice/correction on the looping?

3 Answers 3

2

Move your onSubmit into the tag instead of the Submit button, for one... it's not firing the event at the proper time. It should be like:

<form action="whatever" onSubmit="return submitform();">

Also change the last line of the function,

else document.forms[0].submit();

to

return true;

This should solve the problem of it not running properly, and not intercepting the submission of the form.

Also, you shouldn't have multiple items with the same ID. It's not valid HTML and can cause problems with some browsers *cough*IE*cough*, though you'd probably get away with it in this case.

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

Comments

1

You may want to read the jsfiddle documentation.

Here you'll find a working version of your code. The (corrected) function:

function submitform() {
    var msg = ''
        ,elems = document.getElementById('tasks').getElementsByTagName('input');
    for (var i = 0; i < elems.length; i++) {
        if (elems[i].id && elems[i].id.match(/^t/i) && 
            elems[i].value.replace(/^\s+|\s+$/,'') === '')
        {
            msg += elems[i].id+ ' is empty!\n';
        }
    }
    if (msg.length > 0) {
        alert("one or more of the inputs is/are empty\n"+msg);
        return false
    }
    else alert('we are cool');
    return true;
}

A bit more worked out: http://jsfiddle.net/GbX8m/4/

11 Comments

your code does not execute the submitform() function from the button instead you replace it with a onclick="return false". So how am i suppose to trigger the function?
@pivotal developer: the function runs onload. In jsfiddle.net/GbX8m/2 you can use the submit button too.
@ Kooilnc is it the same if i trigger the function at submit button with onclick="submitform()" instead of the form's onsubmit="submitform()" ?
@pivotal developer: it can have the same effect (alerting a message), but isn't the same. If you use onsubmit, the return value of [submitform] 'decides' if the form gets submitted. If you use onclick on the submit button, you'll have to submit the form programmatically from within the [submitform] function.
@Kooilnc Okie, thanks for clarifying. I also do not really understand the regular expressions you are using. Tried to look online but there's minimal information. what if i want to match id=task and what does this replace(/^\s+|\s+$/,'') means? is it to remove spaces?
|
1
  1. You are not following dom rules. ID should be uniquer for each element. You should have unique id. Read xhtml guidelines.

  2. document.getElementById will return one element not array. If you want to apply on group use function which apply on group like document.getElementsByClassName.

  3. form tab should have action . if you want it to on same page use. action="#"

  4. Fire submit form on form submit not button click and submitform should return true of false depending on if form is valid or not.

  5. If that is for education purpose then good. Otherwise use framework like jquery those have many function which will reduce your work.

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.