In JavaScript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

In JavaScript

how to make a input text contain only one letter maj and three Numbers

10th Jan 2017, 6:06 PM
Garrach_hazem
Garrach_hazem - avatar
50 Answers
+ 12
<input type="text" oninput="validation();"> (include a validation function in JS)
10th Jan 2017, 7:17 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 11
Nah, i believe it's better to spam user when typing… ^_^
10th Jan 2017, 8:54 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 9
…or "oninput"…
10th Jan 2017, 7:08 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 9
<!DOCTYPE html> <html> <head> <title>Validation</title> <script> function validate(){ try { if (document.getElementsByTagName("input")[0].value.toString().match(/\d/g).length==3&&document.getElementsByTagName("input")[0].value.length==4){alert("valid");}else{alert("invalid");} }catch(e){alert("invalid");} } </script> </head> <body> <input type="text" oninput="validate();" placeholder="1 char & 3 nums"> </body> </html>
10th Jan 2017, 7:44 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 9
Ah, yes! do what @visph says… ^_^
10th Jan 2017, 7:51 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 9
@visph when something is incomplete it is never valid!
10th Jan 2017, 8:48 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 9
@visph I just gave him an example hoping he'll replace the "alert" with something else…… ~_~
10th Jan 2017, 8:56 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 9
@visph gave many alternatives and minutes from his life to help… ~_~
10th Jan 2017, 9:04 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 9
I shouldn't give either 'cause you don't know how to use it… ~_~
10th Jan 2017, 9:06 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 9
Well… no-one else would help… we chose you! ~_~
10th Jan 2017, 9:11 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 8
ok, just wait (slow internet)
10th Jan 2017, 7:17 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 8
@Garrach tip : copy-paste ! ^_^
10th Jan 2017, 8:03 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 8
DOESN'T WORK?!?! I checked it! ~_~
10th Jan 2017, 8:14 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 8
Copy - Paste my code, it's working!… ~_~
10th Jan 2017, 8:38 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 8
if you write : "123a" (3 numbers & 1 character) it says "valid"… ~_~
10th Jan 2017, 8:44 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 6
You need to dynamicly handle it by yourself, with the help of javascript... Attach a 'onchange' event function to the input text you want the function control entry user in real-time. Inside this function, define the code to verify / correct the actual 'value" attribut content. Many kind of implementations are possible ;) [ EDIT ] 'onkeypress" event is preferable: check next post for code example and further explanations...
10th Jan 2017, 6:49 PM
visph
visph - avatar
+ 6
HTML: <input type="text" onkeypress="inputHandler(this, filter, correct);"> [ EDIT ] 'oninput' event is more useful in this case ( check next posts ) JS: function inputHandler(src, filterCallback, fixCallback) { if ( ! filterCallback(src.value) ) src.value=correct(src.value); } function filter(val) { /* code for return true if 'val' is valid, else return false */ } function correct(val) { /* code for return corrected value of 'val': simply need to retrieve invalid char */ } Not tested ( means verified this one ), but it's the idea for a generic parametric handler ( you can define and apply how many filter you want ). I change 'onchange' event by replacing it with 'onkeypress" because from memory, it's less problematic ( onchange is only called when entry is validate by the user, meaning when element lost focus, on keypress occurs each time user press a key, so each time the value is supposed to have changed )
10th Jan 2017, 7:20 PM
visph
visph - avatar
+ 6
'oninput' is clearly the better choice, because it's design especially for <input> element ( new event in html5 ). It's first difference/adavantage with keypress/keydown/keyup ( verified just now ) is that it will be fired in case of change due to a paste, or a drop, while key-family event don't ;)
10th Jan 2017, 7:47 PM
visph
visph - avatar
+ 6
I wrote simple example, check it in my codes. "Code for Garrrach_hazem". Try to input letters and numbers I can comment it later because i need to go now.
10th Jan 2017, 8:14 PM
"IceQ" Sergej Kostenko
"IceQ" Sergej Kostenko - avatar
+ 6
@VH species of sadistic! :D
10th Jan 2017, 8:55 PM
visph
visph - avatar