Parsley is a JavaScript library, that can work with both jQuery or Zepto, designed to validate forms easily. It is built with UX concerns in mind and tries to simplify details for the user.
Is this what you're looking for?
Answer from Diego Cardozo on Stack OverflowReplace jQuery Validation Plugin with a dependency-free alternative
JS validator alternatives to JSLint? - javascript
node.js - npm "validator" vs "express-validator" - Stack Overflow
Any alternatives to this simple Javascript validation? - Stack Overflow
Videos
They are used for different scenarios:
validatoris a library to validate any kind of object and it's not associated with any framework.express-validatorusesvalidatorlibrary to validateexpressjsroutes. Basically you can validate express routes out of the box using validator lib.
In my honest opinion you can easily create your own validator middleware. Specially if you are new to Node.js it will help you to understand how middlewares work. If you are not interested on that and you have an express application feel free to use express-validator
Also I really recommend JOI as a validator lib https://github.com/hapijs/joi it's simple and it works well.
Here is an example of a middleware (I have not tested)
const Joi = require('joi')
module.exports = function validate(joiSchema) {
return async (req, res, next) => {
const result = Joi.validate(req.body, joiSchema, {
allowUnknown: true,
abortEarly: false
})
if (result.error) {
throw new result.error
}
await next()
}
}
// express route
router.post(
'/create',
validateMiddleware({
body: {
body: { firstName: Joi.string(), lastName: Joi.string() }
}
}),
(req, res, next) => {
// your logic
})
Express-validator is used as middleware, the idea with express-validator is to check bad input before it reaches your controller.
Now, if you are using validator, you can make your own validation function in which you will simply pass the data and it will tell if the input is in accordance to it (Just like checking an Email, here you can have a function, which will check if input given to it is a function or not).
In case you are using Services in your structure, then you can use validator as I feel it's easy and of more control than express-validator
One alternative is to use the new HTML5 input types and fall back to something like this if the browser does not support the new input type yet.
<input type="number" min="1" max="1000" />
But don't forget you still must do server side validation!
There are many great resources online and on SO on how to do this.
HTML Text Input allow only Numeric input
You can use a regex in your confirms:
var ptrn=/\D/g; // 'D' is any non-digit
if(ptrn.test(b) || b <= 0 || b > 1000){
alert("Please enter only integers between 0 and 1000!");
return cal();
}
is there any lightweight js library to those three for form validation? not everyone is going to use them and just for form validation, it is going to be an overkill.
and bootstrap 5 will drop support for jquery so it is another reason to find and use an alternative.
I am not good enough to write my own js validation code so a lightweight library would be great if there is any