0

I would like to add two functions in javascript such that after first one validates, the second must verify and return the value. But only one is executing in my case:

Here is the code:

<html>
<script type="text/javascript">
function validate(){
var name_value=document.getElementById('qs').value;
if(name_value=="")
{
alert("Please enter your keyword");
}
}
var ray={ajax:function(st)
    {
        this.show('load');
    },
show:function(el)
    {
        this.getID(el).style.display='';
    },
getID:function(el)
    {
        return document.getElementById(el);
    }
}
</script>
<style type="text/css">
#load {
    background: none repeat scroll 0 0 #F7F7F7;
    border: 1px double #999999;
    font-family: "Trebuchet MS",verdana,arial,tahoma;
    height: 50px;
    left: 45%;
    line-height: 50px;
    margin-left: -150px;
    margin-top: -50px;
    position: absolute;
    text-align: center;
    top: 50%;
    width: 350px;
    z-index: 1;
}
</style>
<body>
<div id="load" style="display:none;"><strong>Please wait while we process your request...</strong></div>
<form action="http://www.example.com" method="post" onSubmit="validate() && return ray.ajax()">
<input type="text" value="" name="q" id="qs">
<input type="submit" value="Search">
</form>
</body>
</html>

In the above code, only validate() will work if I remove return ray.ajax() in the <form> tag. But I want both to work i.e. if the textbox isEmpty, show alert, else show Please wait... message.

How to achieve it? Thanks in advance...

1
  • shouldn't the return be before the validate? Commented Aug 20, 2013 at 12:15

1 Answer 1

1

Try

function validate(){
    var name_value=document.getElementById('qs').value;

    if(name_value=="")
    {
        alert("Please enter your keyword");
        return false;
    }
    return true;
}
var ray={
    ajax:function(st)
    {
        this.show('load');
        return true
    },
    show:function(el)
    {
        this.getID(el).style.display='';
    },
    getID:function(el)
    {
        return document.getElementById(el);
    }
}

and

<form action="http://www.example.com" method="post" onSubmit="return validate() && ray.ajax()">
Sign up to request clarification or add additional context in comments.

1 Comment

<form action="http://www.example.com" method="post" onSubmit="return validate() && ray.ajax();"> Yeah just a little change... its working :)

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.