How do i prevent my form from submitting when a user presses enter

 I have a form on my page and i want the user to only be able to submit the form by clicking on the forms's submit button. Because i have specific process for the form submission and when the user presses enter the process is not followed instead the page refreshes.

This is my form

<form class="form-horizontal" onsubmit="myForm()">
   <div class="form-group">
   <input id='nav-search' type="text" class=" form-control" style="width: 100%">
   </div>
</form>

 

message profile
Admin
2022-03-31

This can be achieved easily by adding the atrribute shown below to your form

onkeydown="return event.key != 'Enter';"

Your form should now look like this

<form class="form-horizontal" onsubmit="myForm()" onkeydown="return event.key != 'Enter';">
   <div class="form-group">
   <input id='nav-search' type="text" class=" form-control" style="width: 100%">
   </div>
</form>

Adding the attribute prevents form submission if the user presses enter.

Add Message

Click on the button below to add a new message to this thread

Tags

Thread detail

Satus: Open
Messages: 1Started: 2022-03-31
loading..

DEVMAESTERS

Newsletter

Services

Frontend Development |Backend Development |Full Website Development |Bootstrap Website upgrades | Website Debbugging | Website Hosting & deployment

Contact

Interested in hiring me or collaborating with me on a project, click on any of the links below to get my social media handle

Or contact me via Tel: (+234)-806-225-7480 | Email: abubakarzakari1703@gmail.com

Copywright@devmaesters.com
Privacy Policy

By using our website,
you agree that devmaesters can store cookies on your device and disclose information in accordance with our privacy policy.