ingeni-validation 1.1.4 → 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 +66 -27
- 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
|
+
}
|
|
201
|
+
|
|
202
|
+
// If length is provided
|
|
203
|
+
if (numlength !== null) {
|
|
204
|
+
if (!Number.isInteger(numlength) || numlength <= 0) {
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
203
207
|
|
|
208
|
+
if (numStr.length !== numlength) {
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
204
215
|
|
|
205
216
|
//Pincode validation
|
|
206
217
|
|
|
@@ -220,33 +231,61 @@ async function isValidPin(pinCode) {
|
|
|
220
231
|
}
|
|
221
232
|
|
|
222
233
|
|
|
223
|
-
//Valid Date Time (isValidDate(value,
|
|
224
|
-
|
|
234
|
+
// Valid Date Time (isValidDate(value,"yyyy-mm-dd") , isValidDate(value,"yyyy-mm-dd h:i:s"))
|
|
235
|
+
|
|
236
|
+
async function isValidDate(value, format) {
|
|
237
|
+
|
|
238
|
+
// Reject null or undefined
|
|
239
|
+
if (value === null || value === undefined) return false;
|
|
225
240
|
|
|
226
|
-
|
|
241
|
+
// Reject boolean values
|
|
242
|
+
if (typeof value === "boolean") return false;
|
|
227
243
|
|
|
228
|
-
|
|
244
|
+
// Reject non-string values
|
|
245
|
+
if (typeof value !== "string") return false;
|
|
246
|
+
|
|
247
|
+
value = value.trim();
|
|
248
|
+
|
|
249
|
+
// Reject empty or spaces
|
|
250
|
+
if (value.length === 0) return false;
|
|
251
|
+
|
|
252
|
+
let regex;
|
|
253
|
+
|
|
254
|
+
switch (format.toLowerCase()) {
|
|
255
|
+
|
|
256
|
+
case "yyyy-mm-dd":
|
|
257
|
+
regex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
|
|
258
|
+
break;
|
|
259
|
+
|
|
260
|
+
case "yyyy-mm-dd h:i:s":
|
|
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)$/;
|
|
262
|
+
break;
|
|
263
|
+
|
|
264
|
+
default:
|
|
265
|
+
return false;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (!regex.test(value)) return false;
|
|
269
|
+
|
|
270
|
+
const d = new Date(value.replace(" ", "T"));
|
|
271
|
+
|
|
272
|
+
return !isNaN(d.getTime());
|
|
273
|
+
}
|
|
229
274
|
|
|
230
|
-
case "yyyy-mm-dd":
|
|
231
|
-
regex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/;
|
|
232
|
-
break;
|
|
233
275
|
|
|
234
|
-
case "yyyy-mm-dd h:i:s":
|
|
235
|
-
regex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/;
|
|
236
|
-
break;
|
|
237
276
|
|
|
238
|
-
default:
|
|
239
|
-
return false;
|
|
240
|
-
}
|
|
241
277
|
|
|
242
|
-
|
|
243
|
-
return false;
|
|
244
|
-
}
|
|
278
|
+
//Validate Price
|
|
245
279
|
|
|
246
|
-
|
|
247
|
-
|
|
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;
|
|
248
287
|
}
|
|
249
288
|
|
|
250
289
|
|
|
251
290
|
|
|
252
|
-
export {validateMandate,validateOptional,
|
|
291
|
+
export {validateMandate,validateOptional,isValidMobile,isValidEmail,isValidOtp,isValidPin,isValidNumber,isValidBoolean,checkBooleanParams,isValidDate,isValidPrice};
|