1

ウェブサイト名(ここ)のスペースの使用を制限したい..

<tr>
    <td>
        <h4>Website Name</h4>
    </td>
    <td>
        <input type="text" name="wname" id="wname" size="30" required  onfocus="validateWebsite();" oninput="validateWebsite();">
        <select name="domain">
            <option selected>.com</option>
            <option>.in</option>
            <option>.net</option>
            <option>.org</option>
            <option>.edu</option>
            <option>.int</option>
        </select>
    </td>
</tr>

そして、main.jsファイルには次のコードが含まれています。
ユーザーの入室を制限したい。利用可能なコードでは動作しません:

function validateWebsite()  {
    var s = document.getElementById('wname');
    if (s.indexOf(" ") !== -1){
        wname.setCustomvalidity('');
    }else{
        wname.setCustomValidity('Invalid Website');
    }
}
4

2 に答える 2

3

これ:

if (s.indexOf(" ") !== -1)

する必要があります:

if (s.value.indexOf(" ") === -1)

また、最初のsetCustomvalidityスペルは間違っています(小文字の「v」)。

于 2013-02-14T13:33:22.967 に答える
1

これを試して...

    var s = document.getElementById('wname').value;
    if(s.trim().length == 0 || s.trim().indexOf(' ') != -1)
    {
         // Your code when string is just blank or having spaces
    }
    else
    {
         // Your code when string is not blank and not having any spaces
    }

それが役に立てば幸い...

于 2013-02-14T13:36:57.503 に答える