ingeni-validation 1.1.5 → 1.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ingeni-validation.js +48 -25
- package/package.json +1 -1
package/ingeni-validation.js
CHANGED
|
@@ -180,27 +180,38 @@ async function isValidOtp(otp, otplength) {
|
|
|
180
180
|
return new RegExp(`^[0-9]{${otplength}}$`).test(otpStr);
|
|
181
181
|
}
|
|
182
182
|
|
|
183
|
-
//Valid Number with length
|
|
183
|
+
//Valid Number or with length
|
|
184
184
|
|
|
185
|
-
async function isValidNumber(num, numlength) {
|
|
185
|
+
async function isValidNumber(num, numlength = null) {
|
|
186
186
|
|
|
187
187
|
if (num === null || num === undefined || typeof num === "boolean") {
|
|
188
188
|
return false;
|
|
189
189
|
}
|
|
190
190
|
|
|
191
|
-
if (!Number.isInteger(numlength) || numlength <= 0) {
|
|
192
|
-
return false;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
191
|
const numStr = String(num).trim();
|
|
196
192
|
|
|
197
193
|
if (numStr === "") {
|
|
198
194
|
return false;
|
|
199
195
|
}
|
|
200
196
|
|
|
201
|
-
|
|
202
|
-
|
|
197
|
+
// Check only numeric
|
|
198
|
+
if (!/^\d+$/.test(numStr)) {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
203
201
|
|
|
202
|
+
// If length is provided
|
|
203
|
+
if (numlength !== null) {
|
|
204
|
+
if (!Number.isInteger(numlength) || numlength <= 0) {
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (numStr.length !== numlength) {
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
204
215
|
|
|
205
216
|
//Pincode validation
|
|
206
217
|
|
|
@@ -222,47 +233,59 @@ async function isValidPin(pinCode) {
|
|
|
222
233
|
|
|
223
234
|
// Valid Date Time (isValidDate(value,"yyyy-mm-dd") , isValidDate(value,"yyyy-mm-dd h:i:s"))
|
|
224
235
|
|
|
225
|
-
function isValidDate(value, format) {
|
|
236
|
+
async function isValidDate(value, format) {
|
|
226
237
|
|
|
227
|
-
// Reject null
|
|
228
|
-
if (value === null || value === undefined)
|
|
229
|
-
|
|
230
|
-
|
|
238
|
+
// Reject null or undefined
|
|
239
|
+
if (value === null || value === undefined) return false;
|
|
240
|
+
|
|
241
|
+
// Reject boolean values
|
|
242
|
+
if (typeof value === "boolean") return false;
|
|
231
243
|
|
|
232
244
|
// Reject non-string values
|
|
233
|
-
if (typeof value !== "string")
|
|
234
|
-
|
|
235
|
-
|
|
245
|
+
if (typeof value !== "string") return false;
|
|
246
|
+
|
|
247
|
+
value = value.trim();
|
|
236
248
|
|
|
237
249
|
// Reject empty or spaces
|
|
238
|
-
if (value.
|
|
239
|
-
return false;
|
|
240
|
-
}
|
|
250
|
+
if (value.length === 0) return false;
|
|
241
251
|
|
|
242
252
|
let regex;
|
|
243
253
|
|
|
244
254
|
switch (format.toLowerCase()) {
|
|
245
255
|
|
|
246
256
|
case "yyyy-mm-dd":
|
|
247
|
-
regex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]
|
|
257
|
+
regex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
|
|
248
258
|
break;
|
|
249
259
|
|
|
250
260
|
case "yyyy-mm-dd h:i:s":
|
|
251
|
-
regex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]
|
|
261
|
+
regex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]) ([01]\d|2[0-3]):([0-5]\d):([0-5]\d)$/;
|
|
252
262
|
break;
|
|
253
263
|
|
|
254
264
|
default:
|
|
255
265
|
return false;
|
|
256
266
|
}
|
|
257
267
|
|
|
258
|
-
if (!regex.test(value))
|
|
259
|
-
return false;
|
|
260
|
-
}
|
|
268
|
+
if (!regex.test(value)) return false;
|
|
261
269
|
|
|
262
270
|
const d = new Date(value.replace(" ", "T"));
|
|
271
|
+
|
|
263
272
|
return !isNaN(d.getTime());
|
|
264
273
|
}
|
|
265
274
|
|
|
266
275
|
|
|
267
276
|
|
|
268
|
-
|
|
277
|
+
|
|
278
|
+
//Validate Price
|
|
279
|
+
|
|
280
|
+
async function isValidPrice(price){
|
|
281
|
+
if (price === null || price === undefined) return false;
|
|
282
|
+
if (typeof price === "boolean") return false;
|
|
283
|
+
if (typeof price === "string" && price.trim().length === 0) return false;
|
|
284
|
+
if (isNaN(price)) return false;
|
|
285
|
+
if (Number(price) <= 0) return false;
|
|
286
|
+
return true;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
export {validateMandate,validateOptional,isValidMobile,isValidEmail,isValidOtp,isValidPin,isValidNumber,isValidBoolean,checkBooleanParams,isValidDate,isValidPrice};
|