使用OR语句进行Textarea验证

时间:2017-05-18 13:32:55

标签: jquery textarea

我有一个我无法理解的问题。我想在提交之前检查我的带有#message的Textarea字段是否填写正确。 我想检查两个值是否小于1或者让我们说值“p”然后取消提交但是它不能用于OR语句。我究竟做错了什么?我真的很感激任何帮助。

 $("form").submit(function() {
    if (!$("#message").val().trim().length < 1 || $("#message").val("p") ) // if value will  NOT be less than 1  OR has value p     
    {
        alert("Thank you for sending the message. You should receive confirmation in your mailbox shortly.");
    }
    else {
        $("#labelTextArea").text("Provide a message!").css("color", "red"); 
        $("#message").focus(); //  focus element 
        event.preventDefault();// if msg is clear then error and cancel submission
    }
 });

1 个答案:

答案 0 :(得分:-1)

试试这个:

function validateMyForm() {

   if ($("#message").val().trim().length > 0 && $("#message").val().trim() == 'p' ) // if value will  NOT be less than 1  OR has value p 

    {
    alert("Thank you for sending the message. You should receive confirmation on your mailbox shortly.");
} else {
    $("#labelTextArea").text("Provide a message!").css("color", "red"); 
    $("#message").focus(); //  focus element 
    event.preventDefault();// if msg is clear then error and cancel submission
}
}