我有一个输入文本区域,我希望用户输入他的全名。因此,在该领域至少有两个字是必须的。
但验证码与表单的操作码(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>
答案 0 :(得分:0)
尝试从全名字段值的末尾修剪空白,然后将其拆分并检查数组的长度。
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,'');
}
}