0

In Asp page i have below controls. A input textbox ,Imagebutton and a label.

<input id="txtTotamt"  runat="server" type="text" value="0" />
<asp:ImageButton ID="Validate" runat="server"  OnClientClick="return validatecontrol();"/>
<asp:Label ID="lblerror" runat="server"></asp:Label>

And in javascript in page

<script language="javascript" type="text/javascript">
    function validatecontrol() 
    {
        var valid_amt = document.getElementById("txtTotamt").value;
        if isNaN(valid_amt) == false {
            if(valid_amt % 1 != 0) && (valid_amt>0){
                return true;
            }else{
               document.getElementById("lblerror").innerHTML ="Error";
            }
        }else{
        document.getElementById("lblerror").innerHTML ="Error";
        }
    }
</script>

In code behind

Protected Sub Validate_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles Validate.Click
//my codes go here
End Sub

I want to validate the content of textbox in a JavaScript and I also have code behind for that button click.I want scrip to be executed first and if the text in input text not proper then code behind should not execute. But it does not work for me. I think the code behind in .net gets triggered before the JavaScript. How it can be solved? Is there any error in my javascript?

IT researcher
  • 3,274
  • 17
  • 79
  • 143
  • If there is a problem and you want the form submission to be prevented, you have to stop the default behavior - which is to submit the form. The easiest way to do this would be to "return false" from the js, but that can prevent other events if you are not careful. See [here](http://stackoverflow.com/questions/387736/how-to-stop-event-propagation-with-inline-onclick-attribute) for a proper way to handle this. – Tim Hobbs May 12 '14 at 05:39

5 Answers5

3

Your if statement is missing its brackets. In javascript, all if statements are expected to be wrapped in standard brackets. Without these you will get a syntax error.

the standard is:

if ( /*your if comparison here e.g 1 == 1*/) {

} else {

}

Try changing:

if isNaN(valid_amt) == false {

to:

if (isNaN(valid_amt) == false) {

Same for:

if(valid_amt % 1 != 0) && (valid_amt>0){

to

if((valid_amt % 1 != 0) && (valid_amt>0)){
Cyassin
  • 1,437
  • 1
  • 15
  • 31
  • That solved my problem. Also I included return false wherever required.My actual error was with brackets which i was not aware of. Thank you. – IT researcher May 12 '14 at 06:46
2

Try to replace these lines

 if isNaN(valid_amt) == false {

with

 if (!isNan(valid_amt) || valid_amt!=undefined) {
Dgan
  • 10,077
  • 1
  • 29
  • 51
1

first you have to send to the isNan() function the value of the valid_amt:

if isNaN(valid_amt.value) == false 

and also, in order to avoid calling the server function when the validation is invalid, add return false; to the else statement:

<script language="javascript" type="text/javascript">
    function validatecontrol() 
    {
        var valid_amt = document.getElementById("txtTotamt");
        if isNaN(valid_amt.value) == false {
            if(valid_amt % 1 != 0) && (valid_amt>0){
                return true;
            }else{
               document.getElementById("lblerror").innerHTML ="Error";
               return false;
            }
        }else{
        document.getElementById("lblerror").innerHTML ="Error";
        return false;
        }
    }
</script>
st mnmn
  • 3,555
  • 3
  • 25
  • 32
0
Please modify your script as:

 <script type="text/javascript">
        function validatecontrol() 
        {
            var valid_amt = document.getElementById("txtTotamt");

            if (isNaN(valid_amt) == false)
            {
                if((valid_amt % 1 != 0) && (valid_amt>0))
                {
                    return true;
                }
            else
                {

                   document.getElementById("lblerror").innerHTML ="Error";
 return false;
            }
        }

        else
        {
        document.getElementById("lblerror").innerHTML ="Error";
 return false;
        }
        }
</script>
Aarti
  • 735
  • 5
  • 13
  • 1
    You have essentially copied my answer by just rewriting the entire chunk of code with my two added lines. If you are to copy someone's answer, at least provide something unique such as a deep explanation to why your solution works. – Cyassin May 12 '14 at 05:39
  • You answer is not unique and i have not copied you answer. I just copied the users javascript function andremove the syntax error while running this code and just provide the working code at my end. – Aarti May 12 '14 at 05:52
  • I was the first to comment, so yes my answer was unique. What did you do to remove the syntax code? Replaced the two lines I posted, yuppp looks like a copy. If you have nothing unique to provide to the solution please don't copy or duplicate someone elses. You haven't even provided detail to why your answer is correct. – Cyassin May 12 '14 at 05:57
  • It is not a copy if it looks like a copy to you. When i started answering the question there was no reply at that time . While i was answering may be your post was posted first. Its my wish to provide expalnation or provide the whole answer. – Aarti May 12 '14 at 06:03
  • There is no explanation from you, just an entire copy of the code in the question plus two changes, the exact ones I proposed. You did not provide a whole answer. Ahhh your not worth my time, just learn in the future to remove answers if someone beat you and they are duplicate and don't copy someone unless you can provide a better explanation that could help the person who asked the question. – Cyassin May 12 '14 at 06:10
  • I don't think an explanation required for return statements. If some knows javascript he can easily understand. – Aarti May 12 '14 at 06:15
  • @Nick I agree with you. – IT researcher May 12 '14 at 06:41
0

Two corrections in aspx code ..

  1. OnClientClick="Javascript:return validatecontrol();"
  2. OnClick="Validate_Click"
asp:ImageButton ID="Validate" runat="server"  OnClientClick="Javascript:return validatecontrol();" OnClick="Validate_Click" />

& one correction in Javascript function

use return false; in else part of javascript function. If value is not appropriate then it will stop further processing.

Amnesh Goel
  • 2,617
  • 3
  • 28
  • 47