Html 在表单中避免0值

Html 在表单中避免0值,html,forms,Html,Forms,我有一个表单,其中一个字段到目前为止是一个文本值。我希望避免值0,但仍允许键入分数,如1/3。然后重新计算我的分数并四舍五入到第四位 因此,我不能使用 我该怎么做呢?为什么不对表单进行JavaScript验证呢?它可以让您更精确地了解您接受的数据类型 JavaScript示例: function validateNonzeroForm() { var a = document.forms["myForm"]["nonzero_number"].value; if (a == "0

我有一个表单,其中一个字段到目前为止是一个文本值。我希望避免值0,但仍允许键入分数,如1/3。然后重新计算我的分数并四舍五入到第四位

因此,我不能使用


我该怎么做呢?

为什么不对表单进行JavaScript验证呢?它可以让您更精确地了解您接受的数据类型

JavaScript示例:

function validateNonzeroForm() {
    var a = document.forms["myForm"]["nonzero_number"].value;
    if (a == "0") {
        alert("Nonzero field cannot be zero. Please enter a number.");
        return false;
    }
}
HTML:


请写下号码:
跨浏览器“模式”支持 使用
模式
属性根据所需的
要求验证所有
元素

JavaScript:

function validateInputs(form){

    var all_valid = true
    for(var i=0; i<form.elements.length; i++ ){
        var input = form.elements[i]

    //  Breakpoints:
        //  If we are not looking at an <input> element
        //  If the element does not have a pattern attribute
        if( input.tagName.toLowerCase().indexOf('input') == -1 ) continue;
        if( !input.getAttribute('pattern') ) continue;

    //  input Regex Pattern to test
        var pattern = new RegExp(input.getAttribute('pattern'),'g');

        if( pattern.test(input.value) ){
        //  Passed Test - Do Something
            input.style.background = 'transparent'

        }else{
        //  Failed Test - Do Something
            input.style.background = '#FDD'

            all_valid = false
        }//endifelse

    }//endfor

    if(all_valid)
        return true;

    alert('That is not a valid entry')
    return false

}//endfunction
<form onsubmit="return validateInputs(this);">
    <input type="text" pattern="(^[^0]$)" />
    <button>Go</button>
</form>
function calculateFraction(form){

    function returnError(){
        alert("You must enter a 'non-zero' numeric value")
        input.style.background = '#FDD'
        return false
    }

    //  Define input element to check
        var input = form["fraction"]    //  Input MUST be type="text"

    //  Breakpoint - if value can't be evaluated.
        if( !/^[0-9\/*\.\+-]*$/.test(input.value) )
            return returnError();

    //  Evaluate statement. i.e. compute fractions, etc.
        var value = eval(input.value)

    //  Test for '0'
        if( Number(value)==0 ){
            //  Failed Test - Do Something
            returnError()

            return false
        }

    //  Round to fourth decimal, define as the new input value
        input.value =   Math.round(value*10000)/10000

    //  Submit Form
        return true

}//endfunction
函数验证输入(表单){
var all_valid=true
对于(var i=0;iAvoid 0值在
中),允许键入分数,如1/3,重新计算并将值舍入到第四位小数。
JavaScript:

function validateInputs(form){

    var all_valid = true
    for(var i=0; i<form.elements.length; i++ ){
        var input = form.elements[i]

    //  Breakpoints:
        //  If we are not looking at an <input> element
        //  If the element does not have a pattern attribute
        if( input.tagName.toLowerCase().indexOf('input') == -1 ) continue;
        if( !input.getAttribute('pattern') ) continue;

    //  input Regex Pattern to test
        var pattern = new RegExp(input.getAttribute('pattern'),'g');

        if( pattern.test(input.value) ){
        //  Passed Test - Do Something
            input.style.background = 'transparent'

        }else{
        //  Failed Test - Do Something
            input.style.background = '#FDD'

            all_valid = false
        }//endifelse

    }//endfor

    if(all_valid)
        return true;

    alert('That is not a valid entry')
    return false

}//endfunction
<form onsubmit="return validateInputs(this);">
    <input type="text" pattern="(^[^0]$)" />
    <button>Go</button>
</form>
function calculateFraction(form){

    function returnError(){
        alert("You must enter a 'non-zero' numeric value")
        input.style.background = '#FDD'
        return false
    }

    //  Define input element to check
        var input = form["fraction"]    //  Input MUST be type="text"

    //  Breakpoint - if value can't be evaluated.
        if( !/^[0-9\/*\.\+-]*$/.test(input.value) )
            return returnError();

    //  Evaluate statement. i.e. compute fractions, etc.
        var value = eval(input.value)

    //  Test for '0'
        if( Number(value)==0 ){
            //  Failed Test - Do Something
            returnError()

            return false
        }

    //  Round to fourth decimal, define as the new input value
        input.value =   Math.round(value*10000)/10000

    //  Submit Form
        return true

}//endfunction
HTML

<form onsubmit="return calculateFraction(this);">
    <input type="text" name="fraction" />
    <button>Go</button>
</form>
注意事项

必须是
type=“text”

您必须使用jquery“keypress”方法。或者使用开源插件。