仅验证结束字母

时间:2014-10-30 19:18:36

标签: javascript validation

我想要在用户输入的单词的末尾验证结尾“uu”,“ha”和“cross”...请参阅“脚本”标签中的“//”我的意思:)

这是我的html表单:

<form name="myForm" action="haha.php" onsubmit="return validateForm()" method="post">
    <p id="text">Something: </p> 
    <input type="text" name="form" id="box" >
    <input type="submit" value="Submit" id="but" >
</form> 

<p id="check"></p>

我的java脚本验证......

function validateForm() {
    if( document.myForm.form.value == "" ) {
        document.getElementById('check').innerHTML = 'Please fill in the form:) '; 
        return false;
    } 

    //ok so here's an example of what I'm trying to achieve... 
    if( document.myForm.form.value == endings "uu" "ha" cross" ) --//please correct- endings "uu" "ha" cross" --    { 
        return true;
    } 
}

2 个答案:

答案 0 :(得分:1)

尝试使用正则表达式来测试该字符串是否以给定的子字符串之一结束:

if (/(uu|ha|cross)$/.test(document.myForm.form.value)) {
    return true;
}

答案 1 :(得分:1)

if(submittedValue.slice(-2) == "uu" || submittedValue.slice(-2) ==  "ha" || submittedValue.slice(-5) ==  "cross" )     { 
    return true;
}