0

Why my simple javascript validation not working. I want to check if the default text is same as default value (Keyword(s)), then submit the form with empty parameter.

<form method="get" id="search_form" action="http://somesampleurl.com" onsubmit="return validation();">
<input name="s_rawwords" value="Keyword(s)" id="search_field" class="search_field" type="text">
<input name="s_freeloc" value="City, State or Zip" id="search_field2" class="search_field" type="text">
<input value="" id="search_button" type="submit">
</form>
<script type="text/javascript">
    function validation(){
        var search_key = document.getElementById("search_field").value
        alert(search_key);
        if(search_key =="Keyword(s)"){
            alert("step2");
            search_key = "";
        }
    }
</script>
2
  • 1
    return true or false and it would work Commented May 31, 2013 at 18:59
  • Sorry Guys. It did work. Commented May 31, 2013 at 19:12

2 Answers 2

3

Your function never returns true or false, so it doesn't validate anything. Return false when the inputs are invalid and you don't want the form to be submitted.

See here MSDN documentation

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

Comments

1

Try this code:

function validation(){
    var search_key = document.getElementById("search_field").value;
    alert(search_key);
    if(search_key =="Keyword(s)"){
        alert("step2");
        document.getElementById("search_field").value = "";
    }
    return true;
}

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.