1

I have a textbox and a button, on button's clientClick I call javascript function and there is also server side coding.

Problem is, the page gets post back even if I have return False in the javascript.

Here is my javascript function:

function checkALphaNumericFormat(str) {

            //get previous value before editing
            var txtUserId = document.getElementById('<%=txtUserId.ClientID%>');            
            var userId = txtUserId.value;
            var patternAlphaNumeric = /^[A-z0-9]+$/gi;
            var match = userId.match(patternAlphaNumeric);

            //Check Null values
            if (txtUserId.value != null && txtUserId.value != "") {

                //Check for AlphaNumeric values for User Id
                if (match == null) {
                    alert("Please provide valid AlphaNumeric User Id");
                    return false  ;
                }

                return false ;
            }
            else {
                    alert("User Id field should not be null");
                    return false  ;               
            }              
            return false ;
        }

and I am calling this function on my Form as:

<asp:Button runat="server" ID="btnCreate"  CssClass="loginButton" style="margin:0px 0px 1px 30px;" OnClientClick ="return checkALphaNumericFormat(this.value);"  Text="CREATE" />
3
  • plz provide the button designer where u r calling the on client click Commented Aug 26, 2013 at 6:38
  • call your method like this OnClientClick="return checkALphaNumericFormat(... Commented Aug 26, 2013 at 6:49
  • Thanks guys for the information, I am calling it as <asp:Button runat="server" ID="btnCreate" CssClass="loginButton" style="margin:0px 0px 1px 30px;" OnClientClick ="return checkAlphaNumericFormat(this.value);" Text="CREATE" /> Thanks for your replys. The issue is now solved. Commented Aug 26, 2013 at 7:00

1 Answer 1

7

Try to call JavaScript function as below:

OnClientClick="if(!validateform()){return false;}

where validateform() should be your java script function. Your java script function should have return = true; at the end of function in successfull execution, like in below function:

function validateform()
{
  var txtSearch = document.getElementById('<%=txtKeywordSearch.ClientID%>')
  if(txtSearch.value == '')
  {
  alert('No Search Creatria Selected!');
  return false;
  }
return true; 
}

Please try and let me know if it works for you.

Thanks, Harish

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

2 Comments

Hi Harish, Yes this code also works :) Thanks although we can use return checkAlphaNumericFormat(this.value) as this also works. I have found the bottle, it got stuck at other javascript function. Thanks for your help
welcome user2126933 and thanks to let me know the other way to call the function.

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.