验证html表格单元格并停止导航

时间:2015-05-28 15:57:08

标签: javascript html

我是SO的新手,也是Web开发的新手。我有一个动态HTML表。我想验证输入类型单元格之一并在验证失败时停止导航。下面的代码不起作用。请帮帮我,谢谢!

HTML:

from sys import argv
from os.path import exists

p_script, p_from_file, p_to_file = argv

print ('Copying from {first} to {second}'.format(first = p_from_file, 
                                                 second = p_to_file))

file_to_read = open(p_from_file, 'r')
v_in_file = file_to_read.read()
print (v_in_file)

print ('The input file is {size} bytes long'.format(size = len(v_in_file)))
print ('Does the output file exists? {boolean}'.format(boolean = exists(p_to_file)))
raw_input('Press return to continue...')

v_out_file = open(p_to_file, 'w')
v_out_file.write(v_in_file)

print ('Alright, all done.')
file_to_read.close()
v_out_file.close()

使用Javascript:

<table id="tblpay">
    <thead>
        <tr>
            <th>CUST ID</th>
            <th>Balance Due </th>
            <th>Payment Amt </th>
        </tr>
    </thead>
    <tbody>
        <% while (rs.next()) { %>               
        <tr>
            <td>
                <%=rs.getString("cust_id")%>
            </td>
            <td class="right" id="amount_due">
                <%=rs.getString("amount_due")%>
            </td >
            <td class="right" id="payamt">
                <div id="edit1">
                    <input type="number" id="payinput" class="payc"
                     onchange="return validatedata(this); "  value = <%=rs.getString("amount_due")%> > </input>
                </div>
            </td>                    
        </tr>               
    </tbody>
</table>

1 个答案:

答案 0 :(得分:0)

更改:

 onchange="return validatedata(this); " 

 onkeypress="return validatedata(this); " 

并且:

 function validatedata(evt){

var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode != 46 && charCode > 31
&& (charCode < 48 || charCode > 57))
    return false;

return true;

}

http://jsbin.com/nelujedozi/2/edit