我想使用首字母大寫驗證名稱字段。我正在努力做到這一點,一些我是如何做到這一點,但一點問題。問題是所有輸入類型字段都包含我的這個代碼的第一個字大寫還有電子郵件輸入。這是我的代碼:使用首字母大寫驗證名稱字段
$(document).ready(function() {
$('input').on('keydown', function (e) {
if (this.value == '') {
var char = String.fromCharCode(e.which);
if (char.match(/^\w$/)) {
// If is empty and we pressed a printable key...
this.value = char.toUpperCase();
return false;
}
}
});
$('#00N2800000IA6aX').on('change', function() {
if ($(this).val() == 'India') {
$('#mobile').attr("maxlength", 10);
//set phone value null if you want
$('#mobile').val("");
//set phone value 10 digits and remove rest if you want
$('#mobile').val($('#mobile').val().substr(0, 10));
} else {
$('#mobile').removeAttr("maxlength");
}
});
if ($('#00N2800000IA6aX option:selected').val() == 'India') {
$('#00N2800000IA6aX').trigger('change');
}
$("#abc").click(function() {
var first_name = $('#first_name').val();
var email = $("#email").val();
var mobile = $("#mobile").val();
$("#returnmessage").empty(); // To empty previous error/success message.
// Checking for blank fields.
if (email == '' || mobile == '') {
$("#returnmessage").append("<span>Enter Your Name, Vaild E-mail & Mobile No. <span>");
return false;
}
// To Check Empty Form Fields.
if (first_name.length == 0) {
$('#head').text("* All fields are mandatory *"); // This Segment Displays The Validation Rule For All Fields
$("#first_name");
return false;
}
// Validating Name Field.
if (!first_name.match(name_regex) || first_name.length == 0) {
$('#first_name').text("* For your name please use alphabets only *");
//This Segment Displays The Validation Rule For Name
$("#first_name").focus();
return false;
}
var mailPattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9] {2,4})+$/;
if (!mailPattern.test(email)) {
$("#returnmessage").append("<span>Enter Your Valid Email Address!<span>");
return false;
}
if ($('#00N2800000IA6aX option:selected').val() == 'India') {
var phoneNumberPattern = /^[0-9]{10}$/;
if (!phoneNumberPattern.test(mobile)) {
$("#returnmessage").append("<span>Enter Your 10 Digits Mobile No Only.<span>");
return false;
}
}
});
});
謝謝,但我需要驗證名稱字段 – Overseas
是的,我已經添加驗證代碼以及如果名稱是大寫返回true。如果需要,您可以使用 –
MR。 KD請告訴我在哪裏我會把這個代碼: – Overseas