2011-04-17 6 views
3

私はモバイルサイトを持っており、検証以外はすべて正常に動作しています。基本的には、ユーザーから値を取り出し、別のページ(process.php)で処理したいと考えています。しかし、そうする前に、フィールドにデータが入力されていることを確認する必要があります。私はこれを行ういくつかの方法を見てきましたが、どれもうまくいかないようです。私は現時点で以下のコードを持っています。プロセスボタンを押すと、項目フィールドが空であってもprocess.phpスプラッシュ画面が表示されます。それはデータベースに書き込まれませんが、すべての必須フィールドが入力されるまで、process.php画面にユーザを持ってこなかったでしょう。jQueryモバイルフォーム検証

<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script> 

<script> 
    $(document).ready(function(){ 
    $("#formL").validate(); }); 
</script> 



<div data-role="content"> 

     <form id="formL" action="/website/process.php" method="post"> 
     <div data-role="fieldcontain"> 
     <label for="item"> 
     <em>* </em> <b>Item:</b> </label> 
     <input type="text" id="item" name="item" class="required" /> 
     </div> 

     <div class="ui-body ui-body-b"> 
     <button class="buttonL" type="submit" data-theme="a">Process</button> 
     </div> 
    </form> 

</div> 

答えて

6

このような小さなフォームの場合、プラグインを使用しても構いません。jQuery Mobileと互換性がありますか?とにかく、あなたが始めるために、ここに空のフィールドがある場合に提出することを防止するための簡単な方法です:私はあなたが持っている同じ問題に出会ってきた

$("#formL").submit(function() { 

    // get a collection of all empty fields 
    var emptyFields = $(":input.required").filter(function() { 

     // $.trim to prevent whitespace-only values being counted as 'filled' 
     return !$.trim(this.value).length; 
    }); 

    // if there are one or more empty fields 
    if(emptyFields.length) { 

     // do stuff; return false prevents submission 
     emptyFields.css("border", "1px solid red"); 
     alert("You must fill all fields!"); 
     return false; 
    } 
}); 

You can try it/mess with it here.

+0

お返事ありがとうございます。上記を動かすことができないようです。このコードはどこに置く必要がありますか? #first(ページ)、#second、#thirdへの内部リンクを含むindex.phpファイルが1つあります。私が検証しようとしているフォームは#3番目です。これは外部からデータベースに値を書き込むprocess.phpにリンクします。ありがとう。 – Seven

+0

ありがとう、魅力のように動作します:) – Stiropor

+0

** + 1 **簡単で清潔! 'data-role =" dialog "で動作しないので、jquery.validate.jsよりはるかに優れています。 – Omar

1

、私は自分のフォームが正しく検証しています。

次は、私はjQueryのモバイルでやっていることである - これはしません>

<link rel="stylesheet" href="css/jquery.mobile-1.0a4.1.css" /> 
<link rel="stylesheet" href="css/colors.css"> 
<link rel="stylesheet" href="css/list.css"> 
<!--For Icon to bookmark on phones--> 
<link rel="apple-touch-icon-precomposed" href=""/> 
<script>  

var hdrMainvar = null; 
var contentMainVar = null; 
var ftrMainVar = null; 
var contentTransitionVar = null; 
var stateLabelVar = null; 
var whatLabelVar = null; 
var stateVar = null; 
var whatVar = null; 
var form1var = null; 
var confirmationVar = null; 
var contentDialogVar = null; 
var hdrConfirmationVar = null; 
var contentConfirmationVar = null; 
var ftrConfirmationVar = null; 
var inputMapVar = null; 

// Constants 
var MISSING = "missing"; 
var EMPTY = ""; 
var NO_STATE = "ZZ"; 
</script> 

<div data-role="header" id="hdrMain" name="hdrMain" data-nobackbtn="true"> 

</div> 

<div data-role="content" id="logo" align="center"> 
<img src="img/sam_mobile.png"> 
</div> 

<div data-role="content" id="contentMain" name="contentMain"> 

<form id="form1"> 

<div id="userDiv" data-role="fieldcontain"> 
    <label for="userName">User Name*</label>   
    <input id="userName" name="userName_r" type="text" /> 
</div> 

<div id="passwordDiv" data-role="fieldcontain"> 
    <label for="password" id="passwordLabel" name="passwordLabel">Password*</label>  
    <input id="password" name="password_r" type="password" /> 
</div> 

<div id="submitDiv" data-role="fieldcontain">  
<input type="submit" value="Login" data-inline="true"/> 
</div> 
</form> 
</div><!-- contentMain --> 

<div data-role="footer" id="ftrMain" name="ftrMain"></div> 

<div align="CENTER" data-role="content" id="contentDialog" name="contentDialog">  

    <div>You must fill in both a user name and password to be granted access.</div> 
     <a id="buttonOK" name="buttonOK" href="#page1" data-role="button" data-inline="true">OK</a> 
</div> <!-- contentDialog --> 

     <!-- contentTransition is displayed after the form is submitted until a response is received back. --> 
<div data-role="content" id="contentTransition" name="contentTransition"> 
    <div align="CENTER"><h4>Login information has been sent. Please wait.</h4></div> 
    <div align="CENTER"><img id="spin" name="spin" src="img/wait.gif"/></div> 
</div> <!-- contentTransition --> 

<div data-role="footer" id="ftrConfirmation" name="ftrConfirmation"></div> 


<script> 

$(document).ready(function() { 
//Assign global variables from top of page 
    hdrMainVar = $('#hdrMain'); 
    contentMainVar = $('#contentMain'); 
    ftrMainVar = $('#ftrMain'); 
    contentTransitionVar = $('#contentTransition'); 
    stateLabelVar = $('#stateLabel'); 
    whatLabelVar = $('#whatLabel'); 
    stateVar = $('#state'); 
    whatVar = $('#what'); 
    form1Var = $('#form1'); 
    confirmationVar = $('#confirmation'); 
    contentDialogVar = $('#contentDialog'); 
    hdrConfirmationVar = $('#hdrConfirmation'); 
    contentConfirmationVar = $('#contentConfirmation'); 
    ftrConfirmationVar = $('#ftrConfirmation'); 
    inputMapVar = $('input[name*="_r"]'); 

    hideContentDialog(); 
    hideContentTransition(); 
    hideConfirmation(); 



}); 

    $('#buttonOK').click(function() { 
    hideContentDialog(); 
    showMain(); 
    return false;  
}); 


$('#form1').submit(function() { 
    //Start with false to hide specific div tags 
    var err = false; 
    // Hide the Main content 
    hideMain(); 

    // Reset the previously highlighted form elements 
    stateLabelVar.removeClass(MISSING); 
    whatLabelVar.removeClass(MISSING);    
    inputMapVar.each(function(index){    
     $(this).prev().removeClass(MISSING); 
    }); 

    // Perform form validation 
    inputMapVar.each(function(index){ 
    if($(this).val()==null || $(this).val()==EMPTY){ 
     $(this).prev().addClass(MISSING);    
     err = true; 
     }   
    }); 
    if(stateVar.val()==NO_STATE){   
     stateLabelVar.addClass(MISSING);      
     err = true; 
    }      
    // If validation fails, show Dialog content 
    if(err == true){    
     showContentDialog(); 
     return false; 
    }   

    // If validation passes, show Transition content 
    showContentTransition(); 

    // Submit the form 
    $.post("requestProcessor.php", form1Var.serialize(), function(data){ 
     //DB Validation goes here when we link to the Db 
     confirmationVar.text(data); 
     hideContentTransition(); 
     window.location="access.php"; 
    });   
    return false;  
});  



function hideMain(){ 
    hdrMainVar.hide(); 
    contentMainVar.hide(); 
    ftrMainVar.hide(); 
    } 

    function showMain(){ 
    hdrMainVar.show(); 
    contentMainVar.show(); 
    ftrMainVar.show(); 
    } 

    function hideContentTransition(){ 
    contentTransitionVar.hide(); 
    }  

    function showContentTransition(){ 
    contentTransitionVar.show(); 
    } 

    function hideContentDialog(){ 
    contentDialogVar.hide(); 
    } 

    function showContentDialog(){ 
    contentDialogVar.show(); 
    } 

    function hideConfirmation(){ 
    hdrConfirmationVar.hide(); 
    contentConfirmationVar.hide(); 
    ftrConfirmationVar.hide(); 
    } 

    function showConfirmation(){ 
    hdrConfirmationVar.show(); 
    contentConfirmationVar.show(); 
    ftrConfirmationVar.show(); 
    } 


    </script> 

空のフィールドがある場合は、送信するフォームを選択します。このコードを自由に持ち、好きなだけ操作して遊んでください。ご覧のとおり、私はあなたと同じように.phpファイルを使用して、ユーザーの検証を処理しました。