[英]client and server side validation
嗨,大家好,我想知道如何验证服务器端和客户端这三件事。
数据库表产品
我的数据库看起来像这样,但是其中有400多个数据。
pid name size
1 grey 12 inch
2 blue 16 inch
database table category
pid category
1 vans
2 nike
database table itemised
pid price
1 30.00
2 50.00
我有一些字段需要验证。 我已经进行了验证以检查它是否为空。
我表中的一个字段看起来像这样
<select id="Category" name="Category">
<?php
dbconnect();
$stmt = $conn->prepare("SELECT Name FROM Category WHERE pid=:id");
$stmt->bindParam('id',$id);
$stmt->execute();
$i = 0;
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
if ($i == 0) {
echo '<option SELECTED value="'.$row['Name'].'">'.$row['Name'].'</option>
';
}
else {
echo '<option value="'.$row['Name'].'">'.$row['Name'].'</option>';
}
$i++;
}
?>
</select>
它不是表格形式,而是表格形式。 如果您将size
为数字并写在上面。
我的问题或多或少熟悉开发人员工具以更改发布价值的每个人。 我想同时验证client(JS)和client(php),以确保没有人弄乱值。
我这样做是为了检查示例是否normal
<option value="blue vans">blue vans</option>
not-normal
<option value="">blue vans</option> // in this one the value is `BLANKET`
下面的js检查
function isEmpty(aTextField,errormessage) //null may be used for errormessage to only change the colour of matching fields
{
if (aTextField.value.length<=0)
{
if (lfield != aTextField || lfield === null) { fields[fields.length] = [aTextField,false]; } else { fields[fields.length-1] = [aTextField,false]; }
lfield = aTextField;
if (errormessage !== "")
{
if (counter < error_count || error_count == 0) { errors += errormessage+'\n'; } counter++;
}
return true;
}
else
{
if (lfield != aTextField || lfield === null) { fields[fields.length] = [aTextField,true]; }
lfield = aTextField;
return true;
}
}
在PHP中
function notEmpty($inputname,$error_message)
{
$this->js.='if (!isEmpty(formname.'.$inputname.',"'.$error_message.'")) return false;';
if ( isset($_POST[$inputname]) && ( strlen($_POST[$inputname]) <= 0 ))
{
if ($error_message != null)
{
$this -> error_message .= $error_message.'<br>';
}
}
}
现在您将看到我如何验证表中所有选项的总价值如何在js和php中进行验证
对于size
,如果我末尾没有inch
,那将有点简单,但是要更改数据库中的1000多个数据将很痛苦。
知道我该怎么做吗???
我希望我已经清楚地解释了这一点,如果没有,请发表评论,我将尝试重新表述该问题。
谢谢
我将对Javascript和php使用正则表达式。 您可以在两种语言中使用相同的模式,因此您不必编写两次。
php-检查大小:
<?php
if(!preg_match("/^[0-9]+ (inch|othervalue1|othervalue2)$/",$_POST[$inputname]){
$this -> error_message .= 'Wrong Value<br>';
}
?>
对于JS,您可以看一下: http : //www.w3schools.com/jsref/jsref_obj_regexp.asp
这可能会在JS中达到目的,但尚未经过测试:
function checkSize(value){
var patt=new RegExp("^[0-9]+ (inch|othervalue1|othervalue2)$");
return patt.test(value); //returns false for wrong value
}
此外,我建议将值和单位都设为2列。 它会给您带来几个好处。
您可以使用intval将字符串大小转换为整数,例如intval('12 inch')
将返回12。
尝试preg_match("/(\\d+) inch/", $input_str)
或类似测试您所需的正则表达式是否匹配。
您还应该将所有变量转义(html特殊实体)到HTML输出。 否则,尖括号将破坏页面。
至少您在数据库方面使用了准备好的语句。 三分之一之多似乎对PHP“应用程序”有利。
只是为您提供了另一种验证方式。
您也可以通过ajax发布表单,因此您只需要验证服务器端:
$('#myForm').ajaxForm({
dataType: 'json',
success: function(response) {
if (response.error) {
alert(response.error);
} else {
$('#myForm').text(response.message);
}
}
});
(这是假设您使用的是jQuery插件http://malsup.com/jquery/form )
然后在PHP中,您应该检查当前请求是否为ajax:
function isAjax() {
return !empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'
}
...
if (isAjax() {
$response = array();
if (!empty($this->error_message)) {
$response['error'] = str_replace('<br>', "\n", $this->error_message);
} else {
$response['message'] = 'Form submitted';
}
header('Content-type: application/json');
echo json_encode($response);
exit;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.