How to handle whitespaces in a simple client side form validation? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to handle whitespaces in a simple client side form validation?

I made a simple client side form validation code for a 'name' and 'password' but when i enter whitespaces, it submits. Here is my code: https://code.sololearn.com/WoUtGZsbHv5G/?ref=app

21st Sep 2017, 12:53 AM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
5 Answers
+ 5
- Assign the input value to a variable so you don't need to type form.<input>.value repeatedly. - Use length function to verify it's not empty. - Use IndexOf function to see if there's a space in the input. if (name.length() ==0) { //show warning for empty name } if(password.indexOf(" ")>=0) { //show space not allowed warning } I hope I understand your question correctly. Hth, cmiiw
21st Sep 2017, 5:53 AM
Ipang
+ 4
You must to validate not empty field
21st Sep 2017, 12:58 AM
Daniel
Daniel - avatar
+ 4
@Jonathan, it's okay, I'm a newbie myself mate, anyway, you can check JavaScript string methods in the following link should you be interested. https://www.w3schools.com/js/js_string_methods.asp
21st Sep 2017, 6:25 AM
Ipang
+ 1
Thanks @Ipang. I'm still a newbie so I'm not familiar with IndexOf yet. I didn't assign input values to a variable 'coz i want to understand the logic step by step. Thanks alot. :)
21st Sep 2017, 6:17 AM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
0
try to code it in javascript adding onClick=functionName() inside functionName then get the value of the input and validate it there
21st Sep 2017, 2:17 AM
Joefrey Racal Ramos
Joefrey Racal Ramos - avatar