使用首字母大写验证名称字段

时间:2017-01-13 08:48:17

标签: jquery forms

我想用首字母大写验证名字字段。我正在努力做到这一点以及我如何做到这一点,但有点问题。问题是所有输入类型字段都包含第一个单词大写我的这个代码也是电子邮件输入。这是我的代码:

$(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;
            }
        }
    });
});

2 个答案:

答案 0 :(得分:1)

考虑这个例子..

var x="ebe";

if(x[0].charCodeAt()>=97)
    x[0]=x[0].toUpperCase();

charcodeat给出了ascii值。

答案 1 :(得分:0)

我建议不要强制用户并在代码中处理它。 只需检查名称输入的onchange事件,并在用户更新后将其设置为大写。你可以使用以下功能。

function CapitalizeWord(str)
{
    return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}

用法

$('#first_name').change(function(){
   var text =  $this.val();
   text = CapitalizeWord(text);
   $(this).val(text);
});

如果您只想验证而不设置输入值,可以使用以下功能。此函数将返回true或false,您可以根据返回的标志编写逻辑。

function ValidateCapitalLetter(name)
{
  return /[A-Z]/.test( name[0]);
}

修改

您必须添加上述验证,如下所示

$("#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;
        }

        // Validating Name Field for Capital Letter.
        if (!ValidateCapitalLetter(first_name)) {
            $('#first_name').text("* For your name please add first letter Capital *");

            $("#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;
            }
        }
    });