1

I have a Javascript function like this:

function validatePath() 
{
    var path = document.getElementById('server_path').value;
    if (path.search(":") == -1)
    {
        document.getElementById('path_error').innerHTML="Invalid Server Path!";
    }
    else
    {
        var host_name = path.split(":")[0]
        var regex = new RegExp("^[a-zA-Z0-9.]*$"); 
        if(!(regex.test(host_name)))
        {
            document.getElementById('path_error').innerHTML="Invalid Server Path!";
        } 

    }
}

If the server_path is incorrect it displays the error but the form is still submitted. I don't want user to be able to submit the form until the server_path is correct. How can I do that?

3
  • 2
    write 'return false' after displaying your message. Commented May 30, 2013 at 6:15
  • I'm not even going to go into depth at how duplicated this is google.com/search?q=Disable+form+submit Commented May 30, 2013 at 6:26
  • -1 have you even tried Google? Commented May 30, 2013 at 6:45

3 Answers 3

3

The usual strategy is to use the form's submit handler and return false to stop submission if validation fails:

<form onsubmit="return validatePath();" ...>

Then in the function (pseudocode):

function validatePath() {

  if (validation fails) {
    return false;
  }

  // return any value other than false and the form will submit,
  // including undefined
}
Sign up to request clarification or add additional context in comments.

Comments

1

An other solution:

function validatePath() {
    var path = document.getElementById('server_path').value;
    var submitButton = document.getElementById('submit');

    document.getElementById('path_error').innerHTML='';
    submitButton.removeAttribute('disabled');

    if (path.search(":") == -1){
        document.getElementById('path_error').innerHTML="Invalid Server Path!";
        submitButton.setAttribute('disabled', 'disabled');
    }
    else{
        var host_name = path.split(":")[0]
        var regex = new RegExp("^[a-zA-Z0-9.]*$"); 
        if(!(regex.test(host_name))){
            document.getElementById('path_error').innerHTML="Invalid Server Path!";
            submitButton.setAttribute('disabled', 'disabled');
        } 

    }

 }

See in action: http://jsfiddle.net/rUcQc/.

Comments

0

You can disable the submit button until the path is correct. Like this

<input type='submit' disabled='true' onclick='return validatePath()' id='submit'/>

I made some changes in your javascript code.

function validatePath() {
        var path = document.getElementById('server_path').value;
        if (path.search(":") == -1){
            document.getElementById('path_error').innerHTML="Invalid Server Path!";
        }
        else{
            var host_name = path.split(":")[0]
            var regex = new RegExp("^[a-zA-Z0-9.]*$"); 
            if(!(regex.test(host_name))){
                document.getElementById('path_error').innerHTML="Invalid Server Path!";
            }
              else {
               document.getElementById('submit').disabled=false;
              } 

            }

    }

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.