0

I have textboxes and each textbox contains search button beside that. On Key press i need to validate for alphanumeric characters and if Enter Key is pressed i need to Search. Right Now, My Textbox contains:

<asp:TextBox ID="txtId" runat="server"  TabIndex="1" MaxLength="30" onkeypress="return validateAlphaNumeric(event, this);"></asp:TextBox>
Search Button:
<asp:ImageButton ID="imgSearch" runat="server" ImgUrl=".gif" OnClientClick="returnpassTxtboxValue(imgSearch);"/>

Now if i do:

If (event.KeyCode == 13) then SearchButton.Click();

This executes the returnpassTxtboxValue(imgSearch) function of Search button and then return back to textBox function validateAlphaNumeric() of Textbox. So my test fails by throwing parameterCount exception.

Javascript for textbox:

 function validateAlphaNumeric(evt, textBox) {
        var charCode;
        charCode = (evt.which) ? evt.which : window.event.keyCode;

        if (charCode >= 97 && charCode <= 122 || charCode >= 65 && charCode <= 90 || charCode == 8 || charCode >= 48 && charCode <= 57 || charCode == 45 || charCode == 13) {
            //if (charCode == 13) {
            //    document.getElementById('SearchButton').click();

            //}
            return true;
        }
        else {
            return false;
        }
    }

Can u pls suggest me how to over come this?

Thanks,

2
  • can add the rest of your codes as well ? like your javascript and code behind Commented Jun 6, 2014 at 5:18
  • Javascript for text: added in the above question. Commented Jun 7, 2014 at 9:40

5 Answers 5

1

You should do this way of implementation

    <asp:TextBox ID="txtId" runat="server"  TabIndex="1" MaxLength="30" onkeypress="return validateAlphaNumeric(event, this);"></asp:TextBox>
    Search Button:
    <asp:ImageButton ID="imgSearch" runat="server" ImgUrl=".gif" onclientClick="Validate(this);"/>


$(document).ready(function(e){
  $("#txtId").on("keyup",function(e){

     if(e.keycode == 13){ 
        $("#imgSearch").trigger("click");
     }
  }  
});

function Validate(e){
 e.preventDefault();
 // Do your validation Code here.
}

Thanks

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

2 Comments

e.preventDefault() stops my code : Below error is showing : Object doesn't support property or method 'preventDefault'
Need to add return false after the Search button click in Validation of text box... It worked :) Thanks all :)
0

you have to write below code on your textbox (txtId) keypress event

 if (e.keyCode == 13) $j("#imgSearch").trigger('click');

this code execute button (imgSearch) click event on press enter key

function validateAlphaNumeric(event, obj)
{
   if (event.keyCode == 13) $j("#imgSearch").trigger('click');
}

1 Comment

Can u pls give me the aboce code in Javascript? and i think i have done the same thing in javascript. if(event.keycode == 13){document.getelemntById('imgSearch').Click();
0

Try this

$("#txtId").keypress(function (e) {
    if (e.keyCode == 13) {
    $('#btnId').trigger('click');
    }
});

Comments

0

Search Button:

Please do in following way for a single textbox

$(document).ready(function(){

$("#txtId").keypress(function(e){

if(e.which==10 || e.which==13)
{
$("#imgSearch").click();
}

});

});

Comments

0

Need to add Return false.

if(event.KeyCode == 13){
document.getElementById('SearchButton').click();
return false;
}

It will work...thanks all :)

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.