简体   繁体   中英

Javascript validation form with '[]'

Hey guys i'm newbie with javascript.

I can do with input name 'nim' and function form.nim.value ==""

 <script type="text/javascript">
    function validasi_input(form){
      if (form.nim.value == ""){
        alert("NIM still empty!");
        form.nim.focus();
        return (false);
      }
    return (true);
    }
    </script>
    <form name="frm" method="post" onsubmit="return validasi_input(this)">
    <table>
        <tr>
            <td>NIM</td>
            <td><input class="textbox" type="text" name="nim" /></td>
        </tr>
         <tr>
            <td><input class="tombol" type="submit" name="SUPDATE" value="submit"/></td>
            <td></td>
        </tr>
    </table>
    </form>

but i can't do with input name 'vn[nim]' and form.vn[nim].value ==""

 <script type="text/javascript">
    function validasi_input(form){
      if (form.vn[nim].value == ""){
        alert("NIM still empty!");
        form.vn[nim].focus();
        return (false);
      }
    return (true);
    }
    </script>
    <form name="frm" method="post" onsubmit="return validasi_input(this)">
    <table>
        <tr>
            <td>NIM</td>
            <td><input class="textbox" type="text" name="vn[nim]" value="<?php echo $vn['nim']; ?>" /></td>
        </tr>
         <tr>
            <td><input class="tombol" type="submit" name="SUPDATE" value="submit"/></td>
            <td></td>
        </tr>
    </table>
    </form>

How can i do that with 'vn[nim]' ?, Thanks for any help.

document.querySelector("input[name='vn[nim]']").value == "";

http://jsfiddle.net/DerekL/Trm8G/

Use the bracket syntax instead to access the property value:

form['vn[nim]'].value

Doing this avoids the ambiguity of square brackets being used as array dereferencing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM