-1
私はフォームを構築しており、javascriptでregexで検証しています。最後の関数でregex javascriptスペースでの検証とcharcterの開始時または終了時に - signが使用されない
//Empty field check function
\t function Emptyfield(fld)
\t {
\t \t var error = "";
\t \t if(fld.value.length == 0)
\t \t {
\t \t \t fld.style.background = "Yellow";
\t \t \t error = "input field must not be empty.\n";
\t \t }
\t \t else
\t \t {
\t \t \t fld.style.background = "white";
\t \t }
\t \t return error;
\t }
//username validation function
function validateusername(fld) {
var error = "";
var illegalChar = /^[^-\s][\w]+$/;
if (fld.value == "") {
fld.style.background = 'Yellow';
error = "username field is required.\n";
}
\t else if (illegalChar.test(fld.value)) {
fld.style.background = 'Yellow';
error = "Username contains illegal Characters.\n";
} else {
fld.style.background = 'White';
}
return error;}
// Calling all functions Here
function CheckForm(theForm) {
var reason = "";
reason += validateusername(theForm.username);
/*reason += validateEmail(theForm.pwd);
reason += validateEmail(theForm.email);
reason += validatePhone(theForm.phone);
reason += validateEmpty(theForm.from);/**/
if (reason != "") {
\t alert("Some fields need correction:\n" + reason);
return false;
}
return true;
}
<form name="demo" onsubmit="return CheckForm(this)">
\t \t \t <ul> \t \t
\t \t \t \t <li> <label for="username">Username</label><input type="text" name="username" size="30" class="field" id="username"></li> \t \t
\t \t \t \t <li> <label for="email"> Email</label> <input type="text" name="email" size="30" class="field" id="email"></li>
\t \t \t \t <li> <label for="phone"> Phone No.</label><input type="text" name="phone" size="30" class="field" id="phone"></li>
\t \t \t \t <li> <label for="date"> Date </label><input type="text" name="date" size="30" class="field" id="date"></li>
\t \t \t \t <li> <label for="comment">Comments </label><textarea type="text" name="comment" rows="5" cols="32" class="field" id="comment-box"></textarea></li>
\t \t \t </ul>
\t \t <input type="submit" value = "submit" id="submit-btn">
\t </form>
、私が一番上にあるすべての関数を呼び出しています。
を試すことができます(ハイフン)記号を有効な 'username'ですか? – gurvinder372
スペースを必要とせず、ユーザー名の先頭と末尾に - (ハイフン)記号を使用する –
最後の1つの関数のコード行を削除/コメントアウトする理由+ = validateEmail(theForm.pwd); –