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
5 odpowiedzi
+ 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
+ 4
You must to validate not empty field
+ 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
+ 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. :)
0
try to code it in javascript adding onClick=functionName() inside functionName then get the value of the input and validate it there