3

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.

3 Answers 3

4

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;
  } 
}
Sign up to request clarification or add additional context in comments.

2 Comments

Which can be simplified to a one-line function: return parseFloat(value) === parseInt(value) && !isNan(value);
P.S. Note that the above thinks that "012" is not an integer...don't use parseInt() without specifying the radix: parseInt(value,10)
2

try this

if (isNaN(value))

hope it will work

Comments

0

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".)

1 Comment

Sure. My point was if you provide an example try to have it relate to the question at hand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.