Jquery - First letter should not be Numeric - javascript

In a textbox first character should not be number. So I have following code for this :-
DEMO
$('#FieldName').keyup(function (e) {
if ($(this).val().length === 1 && $.isNumeric($(this).val())) {
$(this).val('');
alert('First letter should not be Numeric.!');
}
});
The above code works but not when typed fast. Try typing fast numbers in the fiddle. It will accept numbers.
What is solution for this?

Try this one. Only takes the first letter of typed text with slice(0,1): and validates.
$('#FieldName').keyup(function(e) {
if ($.isNumeric($(this).val().slice(0, 1))) {
$(this).val('');
alert('First letter should not be Numeric.!');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="FieldName" />

Simple solution with RegEx
$(function() {
$('.notFirst').on('input', function() {
this.value = this.value.replace(/^(\d+)/, '');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="notFirst" />

$('#FieldName').keypress(function (e) {
if ($(this).val().length === 1 && $.isNumeric($(this).val())) {
$(this).val('');
e.preventDefault();
alert('First letter should not be Numeric.!');
}
});
Add e.preventDefault() inside your function.
Demo:
http://jsfiddle.net/b653oo8c/

Since the problem is when the person types fast, I assume it is a performance issue.
To fix it, you can do like this:
$('#FieldName').keydown(function(){
if(this.value.length==1 && this.value<<0==this.value)
{
this.value='';
alert('First letter should not be Numeric.!');
}
});
Your code should run AS FAST AS POSSIBLE inside the event handler!
Using jQuery, you only slow down your code.
Not caching $(this) slows down even more!
I use jQuery just to combat the cross-browser issues, and use the sugested keydown event.
One could use this, and would work just fine:
document.getElementById('FieldName').onkeydown=function(){
if(this.value.length==1 && this.value<<0==this.value)
{
this.value='';
alert('First letter should not be Numeric.!');
}
}
According to #Kaiido, it is a problem when checking the length when the keyup event fires.
If we keep the performance in sight, we could just get rid of the length check:
document.getElementById('FieldName').onkeydown=function(){
if(this.value.charAt(0)+1)//if it is a number, it will add 1, making it check as true
{
this.value='';
alert('First letter should not be Numeric.!');
}
});

Change the keyup() event to keydown(). This should help.
$('#FieldName').keydown(function (e) {
if ($(this).val().length === 1 && $.isNumeric($(this).val())) {
$(this).val('');
alert('First letter should not be Numeric.!');
}
});

Related

Input live check

i got a input field and a button
<input required id="solution" name="solution" type="text" placeholder=""class="form-control input-md">
<button style="background-color:#da251d;" id="sendForm" name="sendForm" class="btn btn-success">Send</button>
Now i want to check the input Field for an value, when the value is incorrect, the button should be disbaled, i try it with js like this
$(document).ready(function(){
$(“#solution”).keyup(function(){
if($(this).val() == ‘value’) {
$(‘#sendForm’).attr(‘disabled’,‘disabled);
}
else {
$(‘#sendForm’).removeattr(‘disabled’);
}
});
});
But it dont work, can you help me pls?
i think you did the typo error on .removeAttr
$(document).ready(function(){
$("#solution").keyup(function(){
if($(this).val() == "value") {
$("#sendForm").attr("disabled","disabled");
}
else {
$("#sendForm").removeAttr("disabled");
}
});
});
You should try to use the "change" or the "input" event. It would look similar to
$('#solution').on('change', function(){
// validation & toggle button state
})
For further event documentations:
change
Easy one!
The code does not work since you're using quotation marks (”) but what you should be using are the String marks (" or ').
The working code:
$(document).ready(function(){
$("#solution").keyup(function(){
if($(this).val() == 'value') {
$('#sendForm').attr('disabled', 'disabled');
}
else {
$('#sendForm').removeAttr('disabled');
}
});
});
Jsbin
Hey this is working fine ,
$("#solution").keyup(function(){
if($(this).val() == 'value') {
$('#sendForm').attr('disabled',false);
}
else {
$('#sendForm').attr('disabled',true);
}
});
You can use attr method for setting disabled property as true or false as per your need.
demo

Convert letter to lowercase letter automatically on keypress?

I want to convert letters automatically on keypress, tried this
$(document).on('keypress', function(e) {
$("#nick").val().toLowerCase();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id=nick value="">
But when I write Uppercase letters, it doesnt convert to lowercase. Where I did mistake ?
You aren't modifying the existing value. You need to re-assign the lowercase value:
$(document).on('keypress', function(e) {
var value = $("#nick").val().toLowerCase();
$("#nick").val(value);
});
Since the keypress event won't change the last character, I would suggest listening to the input event instead. The keyup event would work as well.
Example Here
$(document).on('input', function (e) {
e.target.value = e.target.value.toLowerCase();
});
Without jQuery:
Example Here
document.addEventListener('input', function (e) {
e.target.value = e.target.value.toLowerCase();
});
you gotta use the converted text somewhere, right? :)
$(document).on('keypress', function(e) {
$("#nick").val($("#nick").val().toLowerCase());
});
UPDATE
if you use keyup it'll work as desired: DEMO
$(document).on('keyup', function(e) {
$("#nick").val($("#nick").val().toLowerCase());
});
you can also use below approach.
$(document).on('keyup','#nick',function(e){
$(this).val(e.currentTarget.value.toLowerCase());
})
$(document).on('keyup','#nick',function(e){
$(this).val(e.currentTarget.value.toLowerCase());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="nick" value="">

How to check if value of input is empty after browser refresh?

I want to hide all inputs with class "chang" that contain any value after browser refresh, but entered by the user before refreshing. I tried few things, and nothing works. It's my HTML code:
<input class="chang" id="test1" /> <input class="chang" id="test2" />
And script:
$(document).ready(function() {
if( $(".chang").val().length != 0 ) {
$(this).hide();
}
});
I tried to make it works in Firefox (if it's matter). What am I doing wrong in this case?
$(document).ready(function() {
$('.chang').each(function(index){
if($(this).val().length != 0){
$(this).hide();
}
});
});
Use each() to iterate multiple elements with same class.
Try this:
$(".chang").each(function(){
if($(this).val().length != 0) {
$(this).hide();
}
});
It iterates over each of the .chang elements allowing you to use the $(this) correctly
DEMO
You have to check if the value of the input is empty like this:
if( $(".chang").val() == "" ) {
$(this).hide();
}
fiddle

javascript jquery to check a checkbox if a number greate than 0 is inserted into a box

I have two inputs, text and checkbox:
<input type="text" id="09_1" name="09_1" class="rounded"/>
<input type="checkbox" checked id="h09_1" name="h09_1" class="rounded"/>
the textbox 09_1 is updated with a number by jquery9result of database lookup)
I want the change of the input text to trigger a javascript function that checks the checkbox h09_1.
So something like:
$("#09_1").on("change", function (event) {
alert('test');
});
This alert does not even work so this is not correct but where I am. I obviously want it to check the box rather than alert.
The checking of the checkbox must only happen if the number is greater than 0.
Thanks in advance.
If your code isn't firing, you might try putting the 'on' on the document like so:
$(document).on("change", "#09_1", function (event) {
if(!isNaN(this.value)){
if(this.value > 0)
$('#h09_1').attr('checked','checked');
else
$('#h09_1').removeAttr('checked');
}
});
I think you may want the keyup event
http://jsfiddle.net/steelywing/zkBCx/2/
$('#09_1').on('keyup', function () {
$('#h09_1').prop('checked', +$(this).val() > 0);
});
check out this jsfiddle it runs a value check when the input is changed.
$('#09_1').change(function(){
if($(this).val() > 0){
$('#h09_1').attr('checked','checked');
} else {
$('#h09_1').removeAttr('checked');
}
});

Url validation with jquery

Why in following code don't work this part if(!$(this).val()){... from code and this match is true with http://www.st, i want as: http://stackoverflow.com ?
Example: http://jsfiddle.net/gp9nL/3/
$('.url').keyup(function(){
if (!$(this).val().match(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \?=.-]*)*\/?$/)){
$(this).css("background", "#ffc4c4");
result = false;
}else {
$(this).css("background", "#FFFFEC");
result = true;
}
if(!$(this).val()){
alert('field is empty')
$(this).css("background", "#FFFFEC");
}
return result;
});
This
if(!$(this).val())
is probably not working because it is being called on keyup
$('.url').keyup(function(){
Therefore, there will always be something in the input, as a key stroke is needed to fire keyup and it will never evaluate as empty.
EDIT
As per the comment
What's the solution?
If you want to check if the field is empty, do the check on focusout.
Here is your updated code:
$('.url').focusout(function(){
if(!$(this).val()){
alert('field is empty')
$(this).css("background", "#FFFFEC");
}
});
And your fiddle updated: http://jsfiddle.net/gp9nL/4/

Categories

Resources