Results 1 to 2 of 2

Thread: problem with javascript return false

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    116

    Thumbs down problem with javascript return false

    Dear All

    I have weired situation i just wrote a simple java script function whichh will return true or false

    even when the function returns false my server side button click event was firing.

    this is not happening regullarly

    i am using vs 2010 and ie 8 and vb.net 3.5

    this is not happening all the time , please tell me your sugegstions


    please check my below code

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="ViewRateCard.aspx.vb" Inherits="ViewRateCard" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    <style type="text/css">
    .style1
    {
    width:15%;
    }
    .style2
    {
    width: 10%;

    }
    </style>
    <script type="text/javascript">
    function popNewRateWindow() {
    var url;
    var left = (screen.width / 2) - (400 / 2);
    var top = (screen.height / 2) - (400 / 2);
    url = 'http://localhost:6500/merabell/newRateCard.aspx';
    newwindow = window.open(url, 'name', 'height=200,width=150,top=' + top + ', left=' + left);
    //newwindow = window.open(url, 'name', 'height=700,width=800,top='+top+', left='+left);
    if (window.focus) { newwindow.focus() }
    document.getElementById('btnNew').disabled = true;
    return false;
    }
    function validateDdlist() {
    var validate = false;
    var val;
    var intext
    val = document.getElementById('ddlRateCard').value;
    if (val > -1) {
    validate = true;
    } else {


    }

    val = document.getElementById('ddlAdSize').value;
    if (val > -1 && validate == true) {
    validate = true;
    } else {
    validate = false;


    }


    val = document.getElementById('ddlAdColor').value;
    if (val > -1 && validate == true) {
    validate = true;
    } else {
    validate = false;

    }

    val = document.getElementById('ddlAdFreq').value;
    if (val > -1 && validate == true) {
    validate = true;
    } else {
    validate = false;

    }

    val = document.getElementById('ddlAdPos').value;
    if (val > -1 && validate == true) {
    validate = true;
    } else {
    validate = false;

    }

    val = document.getElementById('txtRate').value;
    if (parseInt(val) > 0 && parseInt(val) != 'NaN' && validate == true) {
    validate = true;
    } else {
    validate = false;

    }

    alert(validate);
    if (validate == false) {

    document.getElementById('divErr').innerHTML = "Please Fill The Mandatory Fields with Correct Values";
    return validate;
    }



    }

    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="Scriptmanager1" runat="server">
    </asp:ScriptManager>
    <table width="100%" >
    <tr>
    <td colspan="6" align="center">
    Test Project
    </td>
    </tr>
    <tr>
    <td class="style2" align="right">
    <div id="div1" runat="server">
    *</div>
    </td>
    <td class="style1" align="left">
    Rate Card:
    </td>
    <td class="style1">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <aspropDownList ID="ddlRateCard" AutoPostBack="true" runat="server">
    <asp:ListItem Selected="True" Value="-1">Create or Select Rate Card</asp:ListItem>
    </aspropDownList>
    </ContentTemplate>
    </asp:UpdatePanel>
    </td>
    <td class="style2" align="right">
    <div id="div2" runat="server">
    *</div>
    </td>
    <td class="style1" align="left">
    Ad Rate:
    </td>
    <td class="style1">
    <asp:UpdatePanel ID="UpdatePanel2" runat="server">
    <ContentTemplate>
    <asp:TextBox ID="txtRate" runat="server"></asp:TextBox>
    </ContentTemplate>
    </asp:UpdatePanel>
    </td>
    </tr>
    <tr>
    <td class="style2" align="right">
    <div id="div3" runat="server">
    *</div>
    </td>
    <td align="left">
    Ad Size:
    </td>
    <td align="left">
    <asp:UpdatePanel ID="UpdatePanel3" runat="server">
    <ContentTemplate>
    <aspropDownList ID="ddlAdSize" runat="server">
    <asp:ListItem Selected="True">Select Ad Size</asp:ListItem>
    </aspropDownList>
    </ContentTemplate>
    </asp:UpdatePanel>
    </td>
    <td class="style2" align="right">
    <div id="div4" runat="server">
    *</div>
    </td>
    <td align="left">
    Ad Color:
    </td>
    <td align="left">
    <asp:UpdatePanel ID="UpdatePanel4" runat="server">
    <ContentTemplate>
    <aspropDownList ID="ddlAdColor" runat="server">
    <asp:ListItem Selected="True">Select Ad Color</asp:ListItem>
    </aspropDownList>
    </ContentTemplate>
    </asp:UpdatePanel>
    </td>
    </tr>
    <tr>
    <td class="style2" align="right">
    <div id="div5" runat="server">
    *</div>
    </td>
    <td align="left">
    Ad Frequency:
    </td>
    <td align="left">
    <asp:UpdatePanel ID="UpdatePanel5" runat="server">
    <ContentTemplate>
    <aspropDownList ID="ddlAdFreq" runat="server">
    <asp:ListItem Selected="True">Select Ad Frequency</asp:ListItem>
    </aspropDownList>
    </ContentTemplate>
    </asp:UpdatePanel>
    </td>
    <td class="style2" align="right">
    <div id="div6" runat="server">
    *</div>
    </td>
    <td align="left">
    Ad Position:
    </td>
    <td align="left">
    <asp:UpdatePanel ID="UpdatePanel6" runat="server">
    <ContentTemplate>
    <aspropDownList ID="ddlAdPos" runat="server">
    <asp:ListItem Selected="True">Select Ad Positon</asp:ListItem>
    </aspropDownList>
    </ContentTemplate>
    </asp:UpdatePanel>
    </td>
    </tr>
    <tr>
    <td align="left">
    </td>
    <td colspan="2" align="center">
    &nbsp;
    </td>
    <td align="left">
    &nbsp;
    </td>
    </tr>
    <tr>
    <td colspan="6" align="center">

    <div id="divErr" runat="server">
    </div>
    </td>

    </tr>
    <tr>
    <td align="left">
    &nbsp;
    </td>
    <td align="center">
    <asp:Button ID="btnNew" runat="server" Text="New Rate Card" OnClientClick="return popNewRateWindow();" />
    </td>
    <td align="left">
    <asp:Button ID="btnSave" runat="server" Text="Save Rate Card" OnClientClick="return validateDdlist();" OnClick="btnSave_Click" />

    </td>
    <td align="left">
    &nbsp;
    </td>
    </tr>
    </table>
    </div>
    </form>
    </body>
    </html>

    even i removed OnClick="btnSave_Click" still the problem is persisting ,please do the need full

    thanks in advance

  2. #2
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: problem with javascript return false

    I'm not sure why returning false is not working unless your validation rules are not correct but I would assume that you would know that since you have an alert in your function. The way I usually do something like this is to initialize my validation flag to true. Then if a validation rule fails, set the flag to false. In the end return the flag.
    Code:
    function validateDdlist() {
    	var validate = true;
    	var val;
    	var intext;
    	
    	val = document.getElementById('ddlRateCard').value;
    	if (val < 0) {
    		validate = false;
    	}
    
    	val = document.getElementById('ddlAdSize').value;
    	if (val < 0) {
    		validate = false;
    	}
    
    	val = document.getElementById('ddlAdColor').value;
    	if (val < 0) {
    		validate = false;
    	}
    
    	val = document.getElementById('ddlAdFreq').value;
    	if (val < 0) {
    		validate = false;
    	}
    
    	val = document.getElementById('ddlAdPos').value;
    	if (val < 0) {
    		validate = false;
    	}
    
    	val = document.getElementById('txtRate').value;
    	if (parseInt(val) < 1 || parseInt(val) == 'NaN') {
    		validate = false;
    	}
    
    	alert(validate);
    	if (validate == false) {
    		intext = "Please Fill The Mandatory Fields with Correct Values";
    	} else {
    		intext = "";
    	}
    	
    	document.getElementById('divErr').innerHTML = "Please Fill The Mandatory Fields with Correct Values";
    	return validate;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width