initial: bootstrap from BukidBountyApp base
This commit is contained in:
79
resources/js/utils/validators.js
Normal file
79
resources/js/utils/validators.js
Normal file
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Checks if an email address is valid.
|
||||
*
|
||||
* @param {string} email
|
||||
* @returns {boolean}
|
||||
*
|
||||
* @example
|
||||
* isValidEmail('test@example.com') // true
|
||||
*/
|
||||
export function isValidEmail(email = '') {
|
||||
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
|
||||
return emailPattern.test(email)
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates Philippine mobile number format.
|
||||
*
|
||||
* Must start with 09 and contain exactly 11 digits.
|
||||
*
|
||||
* @param {string} mobile
|
||||
* @returns {boolean}
|
||||
*
|
||||
* @example
|
||||
* hasValidMobileFormat('09171234567') // true
|
||||
*/
|
||||
export function hasValidMobileFormat(mobile = '') {
|
||||
const pattern = /^09\d{9}$/
|
||||
return pattern.test(mobile)
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a value is numeric.
|
||||
*
|
||||
* Accepts strings or numbers.
|
||||
* Rejects empty strings, whitespace, and NaN.
|
||||
*
|
||||
* @param {string|number} value
|
||||
* @returns {boolean}
|
||||
*
|
||||
* @example
|
||||
* isNumeric('123') // true
|
||||
* isNumeric(45) // true
|
||||
* isNumeric('12.3') // true
|
||||
* isNumeric('') // false
|
||||
* isNumeric(' ') // false
|
||||
*/
|
||||
export function isNumeric(value) {
|
||||
if (value === null || value === undefined) return false
|
||||
if (typeof value === 'string' && value.trim() === '') return false
|
||||
return !Number.isNaN(Number(value))
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks whether a response is a "hash-like" string.
|
||||
*
|
||||
* A valid hash is:
|
||||
* - not empty
|
||||
* - not `true`
|
||||
* - not numeric
|
||||
* - does not contain spaces
|
||||
*
|
||||
* @param {*} response - The value to check
|
||||
* @returns {string|false} - Returns the string if it is a hash, otherwise false
|
||||
*
|
||||
* @example
|
||||
* isResponseAHash('abc123') // 'abc123'
|
||||
* isResponseAHash('123') // false
|
||||
* isResponseAHash(true) // false
|
||||
* isResponseAHash('hello world') // false
|
||||
*/
|
||||
export function isResponseAHash(response) {
|
||||
if (!response || response === true) return false
|
||||
|
||||
const result = String(response)
|
||||
if (result.includes(' ') || !isNaN(result)) return false
|
||||
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user