js校验经纬度

js校验经纬度

validationLatLng.ts

const validation = {
    //经度
    validatorLongitude: (value) => {
        const reg = /^(\-|\+)?(((\d|[1-9]\d|1[0-7]\d|0{1,3})\.\d{0,15})|(\d|[1-9]\d|1[0-7]\d|0{1,3})|180\.0{0,15}|180)$/
        if (value === "") {
            return '请输入经度';
        } else {
            if (!reg.test(value)) {
                return '经度范围:-180~180(保留小数点后十五位)';
            }
            return 'success';
        }
    },
    //纬度
    validatorLatitude: (value) => {
        const reg = /^(\-|\+)?([0-8]?\d{1}\.\d{0,15}|90\.0{0,15}|[0-8]?\d{1}|90)$/
        if (value === "") {
            return '请输入纬度';
        } else {
            if (!reg.test(value)) {
                return '纬度范围:-90~90(保留小数点后十五位)';
            }
            return 'success';
        }
    },

}

export default validation

代码里使用

import validationLatLng from "@/utils/validationLatLng";

let vailLat = validationLatLng.validatorLatitude(latitude);
if (vailLat !== 'success') {
    message.error(vailLat);
    return;
}
let vailLng = validationLatLng.validatorLongitude(longitude);
if (vailLng !== 'success') {
    message.error(vailLng);
    return;
}
js校验经纬度

类似文章

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注