bleustreak12 Posted April 3, 2012 Share Posted April 3, 2012 similar to how we have onfocus and onclick on input tags I am using the below method( if the method is wrong please let me know what would be the proper method ) for validating and submitting a form through javascript when someone presses enter on a textbox in a field in a form Example when someone writes the password in the password field and presses enter (when the cursor is on the password field) the form should validate and submit. I even have a submit button but I want the same thing to happen when enter has been pressed on the textboxes I am using this right now onkeypress=‘if (event.keyCode == 13) { validate(); }’ for my textbox fields the issue over here is that when the history of my text box shows below my text box and I select it by pressing enter it does not take the value first in my text box. It straight away validates the value in the text box Example: I have typed abc in my email textbox, I get abc@xyz.com below my text box showing that I had typed that earlier, now when I do down by pressing the down arrow and pressing enter it does not take abc@xyz.com in my text box but straight away runs validate() and gives me an error as wrong email format. I believe it should first take my old value in the text box and then run the validate function The issue is that it should validate and submit form when enter is pressed in input(textbox) field, but currently it does not select history and run the javascript validation function I am using codeigniter 2.1.0 but I thing this comes in javascript domain Quote Link to comment Share on other sites More sharing options...
scootstah Posted April 3, 2012 Share Posted April 3, 2012 You'll need to submit the form when you press enter, not run validation. Quote Link to comment Share on other sites More sharing options...
nogray Posted April 4, 2012 Share Posted April 4, 2012 If you have a submit button, you don't need to check for the keypress. By default, the form will press the first submit button when the user hit the enter key. To validate the form, your validation function should be on the form.submit event (not the submit button click event). Quote Link to comment Share on other sites More sharing options...
scootstah Posted April 4, 2012 Share Posted April 4, 2012 If you have a submit button, you don't need to check for the keypress. By default, the form will press the first submit button when the user hit the enter key. To validate the form, your validation function should be on the form.submit event (not the submit button click event). He wants to process the form when you press the enter key inside a textarea. By default a new line will be entered when you press enter in a textarea. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 4, 2012 Share Posted April 4, 2012 If you have a submit button, you don't need to check for the keypress. By default, the form will press the first submit button when the user hit the enter key. To validate the form, your validation function should be on the form.submit event (not the submit button click event). He wants to process the form when you press the enter key inside a textarea. By default a new line will be entered when you press enter in a textarea. It sounds more like he is using inputs, not textareas. If you try to do this with a textarea, you will only succeed in pissing people off. Don't break basic functionality. nogray is correct on the current functionality. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.