Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer Validation using javascript

I am using the following code for validating whether the field is empty or not.

<label>Sermon Title</label>
<input class="text-input small-input" type="text" id="sermon_title" name="sermon_title" />
<span id="stitlespansuccess" class="input-notification success png_bg" style="display: none;"></span>
<span id="stitlespanerror" class="input-notification error png_bg" style="display: none;"></span>

    $(document).ready(function () {  
        var submit           = false;
        $('#sermon_title').bind('focusout', function() {
            var sermon_title       = $('#sermon_title').val();
            var pid       = $('#preacher').val();
            if( sermon_title == "") {
                $('#stitlespanerror').html("Title required");
                $('#stitlespanerror').show();
                $('#stitlespansuccess').hide();
                submit = false;
            }
            else
            {
                $('#stitlespanerror').hide();
                $.post("<?= site_url('admin/sermons/uniquetitle/') ?>", { sermon_title: sermon_title,pid:pid },
                function(data){
                    if( "success" == data.trim() ) {
                        $('#stitlespansuccess').show();
                        submit = true;
                    }
                    else
                    {
                        $('#stitlespansuccess').hide();
                        $('#stitlespanerror').html("Title already taken");
                        $('#stitlespanerror').show();
                        submit = false;
                    }
                });
            }
        });
});

I want to check whether the value is integer or not.

like image 367
Natasha Avatar asked Sep 25 '26 04:09

Natasha


2 Answers

This is the correct function to check for integers (e.g. some like: 1.0 etc')

function is_int(value){ 
  if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
    return true;
  } else { 
    return false;
  } 
}
like image 58
Ido Green Avatar answered Sep 26 '26 16:09

Ido Green


try this

if (isNaN(value))

hope it will work

like image 22
asitha Avatar answered Sep 26 '26 17:09

asitha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!