I have this function that validate my form field
export default value => {
let errors = {}
if (!value.name) {
errors.name = 'username is required'
}
return errors
}
How do I trim the value? above code doesn't do that. Is it a good idea to use Object.keys
to iterate and trim all the property values?
this is my solution
export default value => {
value = Object.entries(value).reduce(
(null, [key, value]) => ({ [key]: value.trim() }),
{}
)
let errors = {}
if (!value.name) {
errors.name = 'username is required'
}
return errors
}
yes you can use Object.keys
export default value => {
Object.keys(value).forEach(val => {
value[val] = value[val].trim();
})
let errors = {}
if (!value.name) {
errors.name = 'username is required'
}
return errors
}
Triming object means what your question is not clear. I think your function does job of validation with form data.
var profile = { 'first_name': 'revansiddh', 'last_name': '', 'address': 'Solpaur', 'pincode': '413224' } function validate(obj = {}) { var error = {} Object.keys(obj).forEach((m) => { if (obj[m] == '') { error = { ...error, [m]: m + 'requireed' } } }) return error } console.log("form validation", validate(profile))
Is it what you want achieve ?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.