I have a asp.net form content dropdownlist, textbox and button. I want to implement Enter key press to change focus and check input before submit form. I use onkeyup="EnterPressFocusChange(this, event)" for change focus and OnClientClick="return CheckInputData(this);" for check input data before submit. When I use button property UseSubmitBehavior="false" then change focus task working but checking input function always return true and button onCkick function calls. And when I use button property UseSubmitBehavior="true" then onCkick function calls when I press Enter Key press . How can I complete both task? Below the code that I try.
Aman GuptaPosted Sep 18, 2024, 12:43 PM
Hi A zad,
The issue arises because of how the
UseSubmitBehaviorproperty interacts with JavaScript. WhenUseSubmitBehavior="false", it disables the default form submission behavior, allowing the JavaScript focus change to work but causing your validation function (CheckInputData()) to not behave correctly.To handle both scenarios (focus change and input validation) effectively, you can:
Here’s an updated approach to ensure both the Enter key press moves the focus and input validation works before form submission.
Updated Code:
JavaScript:
ASP.NET Form:
Explanation:
EnterPressFocusChange(a, event):13) is pressed.event.preventDefault()), then moves the focus to the next input element.CheckInputData(a):trueand allows the form to submit.false, preventing form submission.UseSubmitBehavior="false":UseSubmitBehavior="false"to allow our custom JavaScript logic to handle form submission. Without it, pressing Enter would trigger the form submit, bypassing your validation logic.Benefits of This Approach:
This way, you can handle both the focus change and the input validation before submitting the form.
A AzadPosted Sep 17, 2024, 9:53 AM
I already mention that when I use button property UseSubmitBehavior="false" then change focus task working but checking input function always return true and button onCkick function calls.
Naimish MakwanaPosted Sep 17, 2024, 7:09 AM
It looks like you’re trying to handle both focus change on Enter key press and input validation before form submission. The challenge is that
UseSubmitBehavior="false"prevents the default form submission, whileUseSubmitBehavior="true"triggers the form submission on Enter key press.To achieve both tasks, you can handle the Enter key press event in JavaScript and manually trigger the button click if the input is valid. Here’s an updated version of your code:
In this code:
EnterPressFocusChangefunction prevents the default form submission when the Enter key is pressed.tb1), it manually triggers the button click after validating the input.Thanks