简体   繁体   English

验证一个字段的值大于php和javascript中另一个字段的值

[英]validate a field's value is greater than another field's value in php and javascript

I am working with form validations. 我正在使用表单验证。

I have two text fields for adding budget. 我有两个用于添加预算的文本字段。 I want to check whether the value of total budget field is greater than that of daily budget in the click event of my submit button .I am using a form validation jquery plugin for validating my form. 我想检查我的Submit按钮的click事件中的total budget字段的值是否大于每日预算。我正在使用一个表单验证jquery插件来验证我的表单。 I want to customize that jquery with greaterthan rule.How can I validate a field's value is greater than another field's value in php and javascript.. I have tried with a method for greaterthan 我想用大于规则自定义该jquery。如何在php和javascript中验证一个字段的值大于另一个字段的值。

Javascript: 使用Javascript:

method:
greaterThan: function(value, element, param)
    {
    var target = $(param);
    return value <= target.val();
    }

and

rules: {
            totalbudget: {
                   required: true,
                   greaterThan:"#daylybudget"
        },

But this is not working! 但这是行不通的! How can I accomplish this? 我该怎么做?

var val1 = $('#textboxid1').val();
var val2 = $('#textboxid2').val();
var val3 = $('#textboxid3').val();

$('#submitbutton').click(function(){

if((val1 > val2) && (val2 > val3))
{

//prceed further
}
else 
{
alert('alert message');
}

});

NOTE: You need to include jquery before this code.. 注意:您需要在此代码之前包含jquery。

First, javascript is client side, php is server side. 首先,javascript是客户端,php是服务器端。 Depending on the type of validation you need you can implement one of these validations or even both of them. 根据您需要的验证类型,您可以实施这些验证之一,也可以同时实施这两种验证。

Considering the following form 考虑以下形式

<form method="post">
    <input type="text" name="first_num" id="first_num" />
    <input type="text" name="second_num" id="second_num" />
    <input type="text" name="third_num" id="third_num" />
    <input type="submit" value="Send & validate" onclick="validate()" />
</form>

Here's a javascript way of doing it: 这是一种JavaScript方式:

<script type="text/javascript">
    function validate()
    {
        var value1;
        var value2;
        var value3;
        value1 = parseFloat(document.getElementById('first_num').value);
        value2 = parseFloat(document.getElementById('second_num').value);
        value3 = parseFloat(document.getElementById('third_num').value);
        if (value1 > value2 && value2 > value3)
        {
            //we're ok
        }
        else
        {
            alert("Values are not as they should be");
            return false;
        }
    }
</script>

As for the php side: 至于PHP方面:

<?php
    $value1 = $_POST['first_num'];
    $value2 = $_POST['second_num'];
    $value3 = $_POST['third_num'];

    if ($value1 > $value2 && $value2 > $value3)
    {
        //do whatever you want because the values are as you wish
    }
    else
    {
        //the values are not as they should be
    }
?>

Keep in mind that usually, for this kind of validation, javascript might be just enough. 请记住,通常对于这种验证,javascript可能就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 验证输入字段的值大于另一个 - Verifying that the value of a input field is greater than another Javascript:使用对象字段的值来确定另一个对象文字字段的名称 - Javascript: Using object's field's value to determine another object literal's field's name 根据其他字段的值禁用字段 - Disable a field based on another field's value Mongo聚合; 匹配字段的子字段至少有一个大于指定值的数组元素 - Mongo aggregation; match where a field's subfield has at least one array element greater than specified value 如何隐藏div直到某个字段的值大于零 - How to hide a div until a certain field(s) value is greater than nothing 使用Javascript将PHP变量设置为HTML文本字段的值 - Using Javascript for setting PHP variable as HTML text field's value 如何使用vanilla Javascript将一个输入字段的值附加到另一个输入字段的值中? - How to append value of one input field into another input field's value with vanilla Javascript? 输入字段值不应大于其他字段值 - Input field value should not be greater than other field value 如何使用jQuery Validator确定一个字段的值大于另一个字段? - how to use jquery validator to determine value of one field is greater than another field? 根据javascript中字段的值过滤对象 - Filter the object depending on the field's value in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM