aloha_mate
aloha_mate

Reputation: 243

Validate ending letters only

I want too validate endings "uu", "ha" and "cross" at the end of the words that users are going to type in... please see "//" in "script" tags of what I mean:)

Here's my html form:

<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>

And my java script validation...

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;
    } 
}

Upvotes: 2

Views: 79

Answers (2)

dfsq
dfsq

Reputation: 193301

Try to use regular expression to test that string ends with one of the given substrings:

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

Upvotes: 1

Yannick
Yannick

Reputation: 194

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

Upvotes: 1

Related Questions