Reputation: 1228
I have a textbox and I want to have at least 2 letter validation.
I have below code and in which user can insert white space and insert data into the database.
But I want a user at least enter 2 valid letters (char/number) then only he can insert them into the database.
var data = $("#intials").val();
if (data) {
//OK Inseert value in DB
}
else {
alert('please insert value in intials box.');
}
Thanks in advance
Upvotes: 0
Views: 429
Reputation: 23654
Here's a way to do it if you want to be sure the initials are either letters or numbers and are at least 2 characters/numbers long. BTW - I thought maybe intials
was a typo so I changed it to initials
let data = $("#initials").val().trim().replace(/[^0-9a-zA-Z]/gi, '');
if (data.length >= 2) {
// good to go
// data is the value of $("#initials") minus any extra whitespace and without any punctuation
} else {
alert('please insert at least 2 letters (and/or) numbers in the intials box.');
}
Upvotes: 2