我想驗證表單使用我的自定義數據屬性,但不知道如何做到這一點。如果需要,我還想在數據屬性中顯示錯誤消息。使用自定義正則表達式來驗證表單
我正在查找與數據屬性中的正則表達式值相匹配的JavaScript,並將其與相應的輸入值進行匹配。
我有以下形式..
<form id="loginForm" name="loginForm">
<ul>
<li>
<label for="Username">Username:</label>
<input type="email" data-validation-error="Please enter a username" data-validation-use="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$" id="username" name="username" maxlength="254" class="required" />
</li>
<li>
<label for="Password">Password:</label>
<input type="password" data-validation-error="Please enter a password" data-validation-use="^[a-zA-Z]\w{5,12}$" id="password" name="password" value="" maxlength="12" class="required" />
</li>
</ul>
<input type="submit" value="Login" class="button" name="loginBtn" id="loginBtn" />
</form>
我將需要支持舊瀏覽器,所以我可能不包括僅HTML5驗證。我需要使用data屬性,如我的代碼中所示的那樣,因此理想地尋找正則表達式的答案。 – TLD 2011-12-20 12:53:16
此外,我設法從數據屬性中提取正則表達式,但我不確定如何將它與輸入字段進行匹配。這一點的Javascript是我正在尋找的。 – TLD 2011-12-20 12:59:07