Title1

Title2

Title3

4-1 檢查公司編號是否重覆

一、樣板


	    <script type="text/javascript" src="<{xoAppUrl}>modules/ugm_tools2/class/jquery.validate/jquery.validate.min.js"></script>
	    <script type="text/javascript">
	      $( document ).ready( function () {
	        $( "#myForm" ).validate({
	          submitHandler: function(form) {
	             //驗證成功之後就會進到這邊:
	             //方法一:直接把表單 POST 或 GET 到你的 Action URL
	             //方法二:讀取某些欄位的資料,ajax 給別的 API。
	             //此處測試方法一的寫法如下:
	             form.submit();
	          },
	          rules: {
	            no: {
					      required: true,//必填
					      remote: {
					        url: "<{$SCRIPT_NAME}>",
					        type: "post",
					        dataType: "json",           //接受数据格式
					        data: {
					        	op : "check_no",
					        	no : function() {
					            return $( "#no" ).val();
					          }
					        }
					      }
					    }, 
	            title: "required", //必填
	            email: {
	              email: true
	            }
	          },
	          messages: {
	            title: "請填寫公司名稱!",
	            email: "請填寫正確email!",
	            no: {
	            	required : "請填寫公司編號!",
	            	remote : "編號已存在"	
	            }
	          },
	          errorElement: "em",
	          errorPlacement: function ( error, element ) {
	            // Add the `help-block` class to the error element
	            error.addClass( "help-block" );

	            // Add `has-feedback` class to the parent div.form-group
	            // in order to add icons to inputs
	            element.closest( "div.form-group" ).addClass( "has-feedback" );

	            //因checkbox、radio,外面多一層 label
	            if ( element.prop( "type" ) === "checkbox" ||  element.prop( "type" ) === "radio") {
	              error.insertAfter( element.parent( "label" ) );
	            } else {
	              error.insertAfter( element );
	            }

	            // Add the span element, if doesn't exists, and apply the icon classes to it.
	            if ( !element.next( "span" )[ 0 ] ) {
	              $( "<span class='form-control-feedback feedback-no fa fa-times'></span>" ).insertAfter( element );
	            }
	          },
	          success: function ( label, element ) {
	            // Add the span element, if doesn't exists, and apply the icon classes to it.
	            if ( !$( element ).next( "span" )[ 0 ] ) {
	              $( "<span class='form-control-feedback feedback-ok  fa fa-check'></span>" ).insertAfter(  element );
	            }
	          },
	          highlight: function ( element, errorClass, validClass ) {
	            //驗證失敗要做的事
	            //在父親(div)+ has-error - has-success
	            //在後面(span) + glyphicon-remove - glyphicon-ok

	            $( element ).closest( "div.form-group" ).addClass( "has-error" ).removeClass( "has-success" );
	            $( element ).next( "span" ).addClass( "fa-times" ).removeClass( "fa-check" );
	            $( element ).next( "span" ).addClass( "feedback-no" ).removeClass( "feedback-ok" );
	          },
	          unhighlight: function ( element, errorClass, validClass ) {
	            //驗證成功要做的事
	            $( element ).closest( "div.form-group" ).addClass( "has-success" ).removeClass( "has-error" );
	            $( element ).next( "span" ).addClass( "fa-check" ).removeClass( "fa-times" );
	            $( element ).next( "span" ).addClass( "feedback-ok" ).removeClass( "feedback-no" );
	          }
	        });
	        //console.log( $( "#myForm" ).validate());
	      });
	    </script>

二、流程

必須用「json_encode」,才會傳回 true、false


  #----- 檢查編號
  case "check_no":
    echo  json_encode(check_no());
    exit;

三、程式

驗證成功:傳回true ,驗證失敗:傳回false


function check_no(){
   global $xoopsDB;
   $no = system_CleanVars($_REQUEST, 'no', '', 'string');
   if(checkNo("ugm_stk_vendor",$no)){
    ajaxDebug("編號已存在".$no,"check_no"); 
    return false;
  }else{
    ajaxDebug("編號不存在".$no,"check_no"); 
    return true;
  }
}