In my form validation I am try to check for empty strings, which will display one error message, then check that the length is no more than 15 character, which should display a different message. The problem is, the way I have it written, it is only check the length. If I remove it, the empty string check works. It seems it is only check the last thing in my statement. How can I rewrite thig to work for both items?
I tried both this:
const validate = (fieldValues = values) => {
const temp = { ...errors };
if ('name' in fieldValues)
{ temp.name = fieldValues.name ? '' : 'Location Name is required.'; }
{ temp.name = fieldValues.name.length < 16 ? '' : 'Location Name limited to 15 Characters.'; }
setErrors({
...temp
});
if (fieldValues === values)
{ return Object.values(temp).every(x => x === ''); }
};
And this:
const validate = (fieldValues = values) => {
const temp = { ...errors };
if ('name' in fieldValues)
{ temp.name = fieldValues.name ? '' : 'Location Name is required.'; }
if ('name' in fieldValues)
{ temp.name = fieldValues.name.length < 16 ? '' : 'Location Name limited to 15 Characters.'; }
setErrors({
...temp
});
if (fieldValues === values)
{ return Object.values(temp).every(x => x === ''); }
};
Both did the same thing.
adding pipes between just caused an error
3
Answers
Here’s an example of a simple form validation function in JavaScript that checks for empty strings and strings with more than 15 characters:
This function assumes that there is an HTML input element with the id "myInput" and an HTML element with the id "error" to display the error messages.
The function first gets the input value and trims any whitespace from the beginning and end. If the input is empty, it sets the error message to "Please enter a value" and returns false, indicating that validation failed.
If the input is not empty, it checks if the length of the input is greater than 15. If it is, it sets the error message to "The input must be 15 characters or less" and returns false.
If validation is successful, the function clears the error message and returns true, indicating that validation passed.
First of all, the if statements in the second snippet can be simplified a bit:
Now let’s step through that code, assuming
fieldValues.name
is an empty string:if ('name' in fieldValues)' {...
fieldValues.name
is an empty string, but is still present, so'name' in fieldValues
is true. That means we’ll execute the code in the if statement.temp.name = fieldValues.name ? '' : 'Location Name is required.';
fieldValues.name
is''
(empty string), which evaluates to false in JavaScript. So nowtemp.name
is'Location Name is required.'
. Great!temp.name = fieldValues.name.length < 16 ? '' : 'Location Name limited to 15 Characters.';
fieldValues.name.length
is zero, which is less than 16. So now you overwrite the previous error andtemp.name
is now''
.A better implementation would look like:
An alternative to using JavaScript to deal with the validation is to use the built-in browser form validation. Set the input as
required
, and then use input attributes likepattern
to add input boundaries. Then have form notify the user when it’s submitted if those boundaries are violated.In this example the browser adds a pattern to the input to ensure that at least one character is entered, and less than fifteen are entered. Try clicking the button without entering anything in the input. A validation popup appears telling you to fill in the field. Adding more that fifteen characters will reveal a differently worded popup.
Aside: you could also add
maxlength
andsize
to inhibit the visual boundaries of the input field, and ARIA descriptions to make the form more accessible.