How do I prevent the return of enter key from submitting a form

  • 2020-03-30 02:55:35
  • OfStack

How to prevent the enter key from submitting a form is really simple, just one sentence. Onkeydown = "if (event. KeyCode = = 13) return false;" Just write this in the from tag.

If pressing Enter on the page automatically commits, you can set the return value to prevent auto commits such as < Input id="q" name="q" type="text" onkeypress="javascript:return gosearch();" > The return value must be false

 
function gosearch() 
{ 
if(window.event.keyCode == 13) 
{ 
search(); 
return false; 
} 
} 

Or you could just write it in the form
 
<form name="form1" action="action/soft_add_action.php" enctype="multipart/form-data" method="post" onkeydown="if(event.keyCode==13)return false;" onSubmit="return checkSubmit();"> 


Or refer to the following code:


<html>
<head>
<title> The enter key is not allowed to submit the form </title>
</head>
<body>
<form name="myform" action="" onkeydown="if(event.keyCode==13){return false;}">
<input type=text name=user>
<input type=text name=pass>
<input type=button name=mybutton value=submit onclick=myform.submit()>
</form>
 You can be in "myform.submit()" Add form judgment to the function, just like normal form judgment, just change the function name. 
</body>
</html>


So let's talk about event.keycode in javascript.

Keycode 8 = BackSpace BackSpace
Keycode 9 = Tab Tab
Keycode 12 = Clear
Keycode 13 = Enter
16 = Shift_L keycode
17 = Control_L keycode
18 = Alt_L keycode
Keycode 19 = Pause
20 = Caps_Lock keycode
Keycode 27 = Escape Escape
Keycode 32 = space space
Keycode 33 = the Prior
Keycode 34 = Next
Keycode 35 = End
Keycode 36 = Home
Keycode 37 = Left
Keycode 38 = Up
Keycode 39 = Right
Keycode 40 = Down
Keycode 41 = Select
Keycode 42 = Print
43 = the Execute keycode
Keycode 45 = Insert
Keycode 46 = Delete
Keycode 47 = a Help
Keycode 48 = 0 equals braceright
49 = 1 keycode exclam onesuperior
Keycode 50 = 2 quotedbl twosuperior
Keycode 51 = 3 section threesuperior
Keycode 52 is equal to $4
Keycode 53 = 5 percent
Keycode 54 = 6 ampersand
Keycode 55 = 7 slash braceleft
Keycode 56 = 8 parenleft bracketleft
Keycode 57 = 9 parenright bracketright
Keycode 65 is equal to a, a
Keycode 66 is equal to b, b
Keycode 67 = c, c
Keycode 68 = d, d
Keycode 69 = e e EuroSign
Keycode 70 = f, f
Keycode 71 = g, g
Keycode 72 = h h
Keycode 73 = I I
Keycode 74 = j j
Keycode 75 = k, k
Keycode 76 = l, l
Keycode 77 = m m mu
Keycode 78 is equal to n, n
Keycode 79 = o o
Keycode 80 = p, p
Keycode 81 = q, q at
Keycode 82 = r, r
Keycode 83 = s s
Keycode 84 = t, t
Keycode 85 = u, u
Keycode 86 is equal to v, v
Keycode 87 = w w
Keycode 88 is equal to x, x
Keycode 89 is equal to y, y
Keycode 90 = z, z
Keycode 96 = KP_0 KP_0
Keycode 97 = KP_1 KP_1
Keycode 98 = KP_2 KP_2
Keycode 99 = KP_3 KP_3
Keycode 100 = KP_4 KP_4
Keycode 101 = KP_5 KP_5
Keycode 102 = KP_6 KP_6
Keycode 103 = KP_7 KP_7
Keycode 104 = KP_8 KP_8
Keycode 105 = KP_9 KP_9
Keycode 106 = KP_Multiply KP_Multiply
Keycode 107 = KP_Add KP_Add
Keycode 108 = KP_Separator KP_Separator
Keycode 109 = KP_Subtract KP_Subtract
Keycode 110 = KP_Decimal KP_Decimal
Keycode 111 = KP_Divide KP_Divide
Keycode = 112 F1
Keycode 113 = F2
114 = F3 keycode
115 = F4 keycode
116 = F5 keycode
117 = F6 keycode
118 = F7 keycode
119 = F8 keycode
120 = F9 keycode
121 = F10 keycode
122 = F11 keycode
123 = F12 keycode
124 = F13 keycode
125 = F14 keycode
126 = F15 keycode
127 = F16 keycode
128 = f-17 thunder keycode
129 = f-18s keycode
130 = F19 keycode
131 = F20 keycode
132 = F21 keycode
133 = making keycode
134 = moviemaker keycode
135 = F24 keycode
136 = Num_Lock keycode
137 = Scroll_Lock keycode
Keycode 187 = acute grave
Keycode 188 = comma complexity
Keycode 189 = minus underscore
Keycode 190 = period colon
Keycode 192 = numbersign apostrophe
Keycode 210 = plusminus hyphen macron
Keycode 211 =
Keycode 212 = copyright registered
Keycode 213 = guillemotleft guillemotright
Keycode 214 = masculine ordfeminine
Keycode 215 = ae ae
Keycode 216 = cent yen
Keycode 217 = questiondown exclamdown
Keycode 218 = onequarter onehalf threequarters
Keycode 220 = less greater bar
Keycode 221 = plus asterisk asciitilde
Keycode 227 = multiply division
Keycode 228 = acircumflex acircumflex
Keycode 229 = ecircumflex ecircumflex
Keycode 230 = icircumflex icircumflex
Keycode 231 = ocircumflex ocircumflex
Keycode 232 = ucircumflex ucircumflex
Keycode 233 = ntilde ntilde
Keycode 234 = yacute yacute
Keycode 235 = oslash Ooblique
Keycode 236 = aring aring
Keycode 237 = ccedilla ccedilla
Keycode 238 = thorn thorn
Keycode 239 = eth eth
Keycode 240 = diaeresis cedilla currency
Keycode 241 = agrave agrave atilde atilde
Keycode 242 = egrave egrave
Keycode 243 = igrave igrave
Keycode 244 = ograve ograve otilde otilde
Keycode 245 = ugrave ugrave
Keycode 246 = adiaeresis adiaeresis
Keycode 247 = ediaeresis ediaeresis
Keycode 248 = idiaeresis idiaeresis
Keycode 249 = odiaeresis odiaeresis
Keycode 250 = udiaeresis udiaeresis
Keycode 251 = ssharp question backslash
Keycode 252 = asciicircum degree
Keycode 253 = 3 sterling
254 = Mode_switch keycode

Use the keyCode property of the event object to determine the key value for the input

Eg: the if (event. KeyCode = = 13) alert (" enter!" );

The key values correspond to the table
A0X65 U 0 x85
B0X66 V 0 x86
0 x87 C0X67 W
0 x88 D0X68 X
E0X69 Y 0 x89
F0X70 Z 0 x90
G0X71 x48 0 0
1 0 H0X72 x49
2 0 x50 I0X73
J0X74 3 0 x51
K0X75 4 0 x52
X53 L0X76 5 0
6 0 x54 M0X77
N0X78 7 0 x55
8 0 x56 O0X79
P0X80 9 0 x57
Q0X81ESC0X1B
R0X82CTRL 0 x11
S0X83SHIFT0X10
T0X84ENTER0XD

If you want to use a combination of keys, you can use event.ctrlkey, event.shiftkey, event.altkey to determine whether the CTRL, shift, and Alt keys are being pressed.


Related articles: