使用div与Jquery进行输入验证

时间:2012-07-12 16:35:59

标签: jquery

我希望在div而不是警告中显示我的输入验证。我目前有

//check for COGs to be a number
        if (isNaN($("#txtCOGs").val())) {
            alert("The Cost of Goods Sold must be a number");
            $("#txtCOGs").focus();
            return false;
        }

我想把它放在div

<div id="divError">

么?

2 个答案:

答案 0 :(得分:3)

$('#divError').text("The Cost of Goods Sold must be a number");

或包含html:

$('#divError').html('<span style="font-style:italic">The Cost of Goods Sold</span> must be a number');

或者如果您想在同一个div中附加多个错误消息:

$('#divError').append("The Cost of Goods Sold must be a number<br />");
$('#divError').append("Another error message...<br />");

答案 1 :(得分:0)

//check for COGs to be a number
        if (isNaN($("#txtCOGs").val())) {
            $("#divError").text("The Cost of Goods Sold must be a number");
            $("#txtCOGs").focus();
            return false;
 }

使用此HTML代码:

<div id="divError"></div>