Jump to content

validate and submit form when enter is pressed in input(textbox) field


bleustreak12

Recommended Posts

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

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.