开发者

Simple JQuery validation

开发者 https://www.devze.com 2023-02-20 11:15 出处:网络
Just after a check to make sure one hidden field is not empty. if($(\'#cal_startdate\').val(\'\')) { alert(\'Please select a start date\') }

Just after a check to make sure one hidden field is not empty.

if($('#cal_startdate').val('')) {
        alert('Please select a start date') }
        else { form.submit }

That isn't quite right. I'd rather it was an onsubmit action.

Any ideas,

Marvellous

E开发者_开发技巧DIT


What about this:

$('#formId').submit(function(){
  if($('#cal_startdate').val() == ""){
    alert('Please select a start date');
    return false; // To avoid the submit (error)
  }
});


You'll want to attach the validation to the submit() event handler. If data is valid, return true - else, return false.

$('form').submit(function(){
    if ($('#cal_startdate').val().length==0){
        alert('Please Enter Start Date');
        return(false);
    }
    return(true);
});


$("form").submit(function(){
    if($("#cal_startdate").val() === ""){
        alert("Please select a start date");
        return false;
    })
})


Beware, here you are using val() as a setter. Effectively, calling <selection>.val('') will set your input to something empty. What you want to do is the following:

if($('#cal_startdate').val() === '')
  alert('Please select a start date');
else
  form.submit();

That should work now

0

精彩评论

暂无评论...
验证码 换一张
取 消