Tuesday, January 8, 2008

Using JavaScript to prevent or trigger form submission when ENTER is hit on textbox

for Preventing:

Suppose there is a textbox control called txtDemo. Add below line of code in the page_load method.

txtDemo.Attributes.Add("onkeypress", "return noenter()");


< script type="text/javascript" >
function noenter() {
return !(window.event && window.event.keyCode == 13); }
< /script >


For Triggering:

Option 1:

Suppose there is a textbox control called txtDemo and a button control namely btnSubmit. Add below lines of code in the page_load method.

txtDemo.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
btnSubmit.Attributes.Add("onclick", "return validate_Data();");


< script type="text/javascript" >

function clickButton(e, buttonid)
{
var bt = document.getElementById(buttonid);
if (typeof bt == 'object')
{
if (event.keyCode == 13)
{
bt.click();
return false;
}
}
}
< /script >


Option 2:

In case of IE4+

Suppose there is a textbox control called txtDemo. Add below line of code in the page_load method.

txtDemo.Attributes.Add("onkeypress", "return entsub(this.form)");


< script type="text/javascript" >
function noenter() {
return !(window.event && window.event.keyCode == 13); }
< /script >

In Case of Netscape Navigator

txtDemo.Attributes.Add("onkeypress", "return entsub(event,this.form)");


< script type="text/javascript" >
function entsub(event,ourform) {
if (event && event.which == 13)
ourform.submit();
else
return true;}

< /script >

1 comment:

cdgnfg said...

Today is the gold für wow second day of 2009 ,world of warcraft gold it also a mesos special for me .cheap wow gold Because i have cheap maplestory mesos a chance to go to an english speech of LiYang and crazy to learn english follow him . He is a firendly,kindly person who make me feel good.wow gold kaufen What‘s more ,maple story mesos he very confidence .And he make me sure what he can do i also can do,as long as i make a determination and force myself to do it every second,Crazy just like him .wow geld I learn one setence from his book ever :maple story items If you want to succeed always force yourself to do more .I can't agree more with him .wow gold farmen To be honest ,when i was a littel girl i already fall in love with english.But what a pity i am poor in english ,Maple Story Account and it make me feel frustrating .So i want to give up many times ,but i can't as i still love it .So i tell mysefl :if you think you can you can wow leveling,and all your maple story power leveling hard work will pay off.wow power leveling Today i am very happy i can listien this wonderful speech of LiYang. I reap a great benifits from him .maple story money As he say :i am the best ,and every one can do it . world of warcraft power leveling Yes,i belive i can do it if i crazy as he