我正在使用以下代码来验证该字段是否为空。使用javascript的整数验证
<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;
}
});
}
});
});
我想检查该值是否为整数。
可将其简化为单行函数:'return parseFloat(value)=== parseInt(value)&&!isNan(value);' – nnnnnn 2012-01-02 09:20:46
P.S.请注意,上面认为“012”不是一个整数......不使用parseInt()而不指定基数:'parseInt(value,10)' – nnnnnn 2012-01-02 09:47:42
好点 - 您需要将基数添加到parseInt 。 – 2012-01-02 11:35:07