Natasha
Natasha

Reputation: 980

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.

Upvotes: 3

Views: 825

Answers (3)

nnnnnn
nnnnnn

Reputation: 150010

You want to check whether what value is an integer? Surely not Sermon Title? There's a lot of code there that doesn't seem at all related to your question.

Anyway, assuming you want to validate a user input value to be sure it is an integer, you can convert it to a number and then test whether the number is equal the same number rounded down:

function isInteger(val) {
   var n = +val;    // convert with unary plus operator
   return val != "" && n === Math.floor(n);
}

Note that an empty string will convert to 0, hence the test that val is not an empty string.

Potential problem: The above would treat "1.000" as an integer - is that acceptable?

If you don't want the user to enter a decimal point another way to approach it is to validate with a regex, making sure that only digits have been entered (no decimal point):

function isInteger(val) {
   return /^-?\d+$/.test(val);
}

If you need to restrict it to a certain number of digits this is easy with regex:

// e.g., max four digits, optional minus sign:
return /^-?\d{1,4}$/.test(val);

(Note: `isNaN() is "broken".)

Upvotes: 0

Ido Green
Ido Green

Reputation: 2813

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;
  } 
}

Upvotes: 4

asitha
asitha

Reputation: 219

try this

if (isNaN(value))

hope it will work

Upvotes: 2

Related Questions