国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

座標(biāo)驗(yàn)證
P粉982009874
P粉982009874 2023-07-28 09:27:27
0
1
699
<p>我有一個(gè)用於坐標(biāo)的文本字段,我想使用vee-validate(3.x)和Vue 2對(duì)其進(jìn)行驗(yàn)證。我嘗試了兩種不同的方法,但都沒(méi)有成功。座標(biāo)的格式應(yīng)該是"整數(shù)或浮點(diǎn)數(shù),整數(shù)或浮點(diǎn)數(shù)",即"緯度,經(jīng)度"(只有一個(gè)逗號(hào),多個(gè)逗號(hào)應(yīng)該被標(biāo)記為無(wú)效)。 </p><p>這是文字欄位:</p><p><br /></p> <pre class="brush:html;toolbar:false;"><ValidationProvider v-slot="{ errors }" rules="coordinates_validation"> <v-text-field :label="$t('stations.position')" :value="positionValue" :error-messages="errors" @input="$emit('update:station', { ...station, ...getLatLong($event) })" @keypress="justNumber" /> </ValidationProvider> </pre> <p>這是我嘗試過(guò)的兩種方法,但都沒(méi)有成功:</p> <pre class="brush:js;toolbar:false;">extend("coordinates_validation", { validate: (value) => { const coordinates = value.split(","); if (coordinates.length !== 2) { return false; } const trimmedCoordinates = coordinates.map((coord) => coord.trim()); const isValidCoordinate = (coord) => { return !Number.isNaN(parseFloat(coord)) && Number.isFinite(coord); }; return ( trimmedCoordinates.every(isValidCoordinate) && !trimmedCoordinates.some((coord) => coord === "") ); }, message: i18n.tc("validations.coordinates_incorrect_format"), }); </pre> <pre class="brush:js;toolbar:false;">extend('coordinates_validation', { validate: (value) => { const regex = /^d (.d )?,s*d (.d )?$/; return regex.test(value); }, message: i18n.tc('validations.coordinates_incorrect_format'), }); </pre> <p>有人知道如何解決這個(gè)問(wèn)題嗎? </p>
P粉982009874
P粉982009874

全部回覆(1)
P粉682987577

當(dāng)你不確定時(shí),可以透過(guò)console.log來(lái)查看輸出。

請(qǐng)查看我的codesandbox,我在驗(yàn)證過(guò)程中使用console.log來(lái)分步驟地輸出,必要時(shí)將大步驟拆分為小步驟。

對(duì)於你的第一次嘗試,問(wèn)題在於Number.isFinite總是會(huì)回傳false。這是因?yàn)槟憬o它傳遞了一個(gè)字串coord,但Number.isFinite期望的是一個(gè)數(shù)字。修復(fù)方法如下:

#
Number.isFinite(parseFloat(coord))

你的初始值.split只適用於逗號(hào)","。我建議在空格、逗號(hào)和逗號(hào) 任意數(shù)量的空格上進(jìn)行分割。

#
const coordinates = value.split(/[,\s]\s*/);

對(duì)於你的第二次嘗試,只使用正規(guī)表示式,我沒(méi)有看到任何問(wèn)題。我將程式碼放在同一個(gè)codesandbox中,並將其用作驗(yàn)證方法時(shí),它完美地工作。

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板