[英]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.