0

ユーザーにフルネームを入力してもらいたい入力テキスト領域があります。そのため、フィールドには少なくとも 2 つの単語が必要です。

しかし、検証コードはフォームのアクション コード (action="nome.php") と競合しています。

どうすれば修正できますか?

コードは次のとおりです。

<script type="text/javascript">
// <![CDATA[
function validateMe(form_elm)
{
var full = form_elm.user.value;
// clear any spaces in front
while (full.charAt(0) == " ") full = full.substr(1);
// split and check for two names - dual purpose of also retrieving first and last name for future use
full = full.split(" ");
var first_name = full[0];
var last_name = full[1];
if (full.length < 2) alert("Please enter your full name.");
return false;
}
// ]]>
</script>

<form method="post" name="input" action="nome.php" onsubmit="return validateMe(this)">
Fazer reserva em nome de:
<input name="user" type="text"/>
<input type="submit" name="Submit" value="NEXT" />
</form>
4

1 に答える 1

0

氏名フィールドの値の末尾から空白を削除してから、それを分割して配列の長さを確認してください。

http://jsfiddle.net/bBZry/

function validate(el) {
    var text = el.user.value.trim();
    console.log(text);
    var words = text.split(" ");
    if (words.length > 1) {
     return true;   
    } else {
     return false;   
    }
}

if (!String.prototype.trim){
    String.prototype.trim = function(){
        return this.replace(/^\s+|\s+$/g,'');
    }
}
于 2013-08-02T19:03:50.900 に答える