php - 使用 javascript 进行整数验证

标签 php javascript validation codeigniter

我正在使用以下代码来验证该字段是否为空。

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

我想检查该值是否为整数。

最佳答案

这是检查整数的正确函数(例如:1.0 等)

function is_int(value){ 
  if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
    return true;
  } else { 
    return false;
  } 
}

关于php - 使用 javascript 进行整数验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8698645/

相关文章:

php - 这个准备好的陈述有什么问题

php - 尝试显示随机选择的 id 中的 mySQL 数据

javascript - 针对 NodeList 调用 Array.prototype.slice 的目的是什么?

javascript - 为什么 javascript 替换会抛出错误

html - 空字段没有验证样式

asp.net-mvc - TinyMCE客户端验证问题

php - 这两个功能对于 sanitizer 来说是否过大?

php - 无法从 Codeigniter 中的模型获取返回值

javascript - 在数据表的onclick函数中传递两个以上的参数

asp.net-mvc - datatype.text 验证什么?