Reputation: 13
I want to validate Name field with first letter capital. I am trying to do this and some how I was done it but little bit issue. Issue is all input type field are contain first word capital with my this code also email input. Here is my code:
$(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;
}
}
});
});
Upvotes: 1
Views: 2516
Reputation: 11
Consider this example..
var x="ebe";
if(x[0].charCodeAt()>=97)
x[0]=x[0].toUpperCase();
charcodeat gives the ascii value.
Upvotes: 1
Reputation: 5989
I would recommend not to force user and handle it in your code. Just check onchange event of your name input and make it capitalize once user update it. you can use following function to do so.
function CapitalizeWord(str)
{
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
Usage
$('#first_name').change(function(){
var text = $this.val();
text = CapitalizeWord(text);
$(this).val(text);
});
You can use following function if you just want to validate and not to set the value of input. This function will return you true or false and you can write your logic as per the flag returned.
function ValidateCapitalLetter(name)
{
return /[A-Z]/.test( name[0]);
}
EDIT
You have to add above validation as following
$("#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;
}
}
});
Upvotes: 0