Javascript 为什么这个正则表达式在传递到php/mysql时失败? 公共静态bool CheckFields(如\u AccountInfo regInfo、字符串密码confirm、, 字符串电子邮件确认,参考字符串错误消息) { errorMessage=“”; //验证字段中的数据(确保它与其类型匹配 如果(!regInfo.fields.CheckMySQLFields(ref errorMessage)) 返回false; 如果(regInfo.GetFieldValue(“密码”)==null | | (AS_Preferences.askUserForEmail和®Info.GetFieldValue(“email”)==null)) { errorMessage=“帐户信息设置不正确..!缺少字段..!”; 返回false; } //密码必须匹配 if(regInfo.GetFieldValue(“密码”)!=passwordConfirm){ errorMessage=“密码必须匹配…”; 返回false; } //如果已输入电子邮件 else if(AS_Preferences.askUserForEmail&®Info.GetFieldValue(“电子邮件”)!=“”){ //它必须是有效的 如果(!new Regex(@“^[\w!\$%&*+\-/=?\^ `{124;}~++(\.[\ w!\\$%&*+\-/=?\^ `{124;}~+])*” +“@leeds.ac.uk”).Match(regInfo.GetFieldValue(“电子邮件”)。成功){ errorMessage=“无效的电子邮件…”; 返回false; } //并匹配其确认 如果(regInfo.GetFieldValue(“email”)!=emailConfirm){ errorMessage=“电子邮件必须匹配…”; 返回false; } } //一切都好。。! 返回true; }

Javascript 为什么这个正则表达式在传递到php/mysql时失败? 公共静态bool CheckFields(如\u AccountInfo regInfo、字符串密码confirm、, 字符串电子邮件确认,参考字符串错误消息) { errorMessage=“”; //验证字段中的数据(确保它与其类型匹配 如果(!regInfo.fields.CheckMySQLFields(ref errorMessage)) 返回false; 如果(regInfo.GetFieldValue(“密码”)==null | | (AS_Preferences.askUserForEmail和®Info.GetFieldValue(“email”)==null)) { errorMessage=“帐户信息设置不正确..!缺少字段..!”; 返回false; } //密码必须匹配 if(regInfo.GetFieldValue(“密码”)!=passwordConfirm){ errorMessage=“密码必须匹配…”; 返回false; } //如果已输入电子邮件 else if(AS_Preferences.askUserForEmail&®Info.GetFieldValue(“电子邮件”)!=“”){ //它必须是有效的 如果(!new Regex(@“^[\w!\$%&*+\-/=?\^ `{124;}~++(\.[\ w!\\$%&*+\-/=?\^ `{124;}~+])*” +“@leeds.ac.uk”).Match(regInfo.GetFieldValue(“电子邮件”)。成功){ errorMessage=“无效的电子邮件…”; 返回false; } //并匹配其确认 如果(regInfo.GetFieldValue(“email”)!=emailConfirm){ errorMessage=“电子邮件必须匹配…”; 返回false; } } //一切都好。。! 返回true; },javascript,c#,php,regex,Javascript,C#,Php,Regex,我认为有些括号放错了地方。缩进并美化了PHP代码的一部分: public static bool CheckFields(AS_AccountInfo regInfo, string passwordConfirm, string emailConfirm, ref string errorMessage) { errorMessage = ""; // Validate the data in the fields (make sure it matches their

我认为有些括号放错了地方。缩进并美化了PHP代码的一部分:

public static bool CheckFields(AS_AccountInfo regInfo, string passwordConfirm, 
string emailConfirm, ref string errorMessage)
{

    errorMessage = "";

    // Validate the data in the fields (make sure it matches their type
    if ( !regInfo.fields.CheckMySQLFields (ref errorMessage) )
        return false;

    if (regInfo.GetFieldValue("password") == null || 
        (AS_Preferences.askUserForEmail && regInfo.GetFieldValue("email") == null))
    {
        errorMessage = "Account info not set up correctly..! Missing fields..!";
        return false;
    }

    // Password must match
    if (regInfo.GetFieldValue ("password") != passwordConfirm) {
        errorMessage = "Passwords must match..!";
        return false;
    }
    // If an email has been entered
    else if (AS_Preferences.askUserForEmail && regInfo.GetFieldValue ("email") != "") {
        // It must be valid
        if (!new Regex (@"^[\w!#$%&*+\-/=?\^_`{|}~]+(\.[\w!#$%&*+\-/=?\^_`{|}~]+)*@"
            + "@leeds.ac.uk").Match (regInfo.GetFieldValue ("email")).Success) {
            errorMessage = "Invalid email..!";
            return false;
        } 

        // And match its confirm
        if (regInfo.GetFieldValue ("email") != emailConfirm) { 
            errorMessage = "Emails must match..!";
            return false;
        } 
    }

    // All good..!
    return true;
}

 <? php
   $link = try_mysql_connect($databaseHostname, $databaseUsername, 
$databasePassword, $databaseDbName, $databasePort);

$from = $emailAccount;

$info = $_POST['newAccountInfo'];
$requireEmailActivation = strtolower($_POST['requireEmailActivation']);

$fields = explode($fieldsSeparator, $info);


$fieldNames = "";
$fieldValues = "";
$email = "";
foreach ($fields as $field) {

$words = explode($fieldNameValueSeparator, $field);

$fieldName  = $words[0];
$fieldValue = $words[1];

$fieldName = stripslashes($fieldName);
$fieldValue = stripslashes($fieldValue);
$fieldName  = mysqli_real_escape_string($link,$fieldName);
$fieldValue = mysqli_real_escape_string($link,$fieldValue);

if ($fieldName == "")
continue;

}

if ($requireEmailActivation == "true") {
    if ($fieldName == "username")
    $username = $fieldValue;
    if ($fieldName == "email"){
    $email = $fieldValue;
}

// Store it
$fieldNames  .= "`$fieldName`,";
if ($fieldValue == "")
$fieldValues .= "NULL,";
else
    $fieldValues .= "'$fieldValue',";

 }
$fieldNames = rtrim( $fieldNames, ",");
$fieldValues = rtrim( $fieldValues, ",");

$query = "INSERT INTO accounts 
(" . $fieldNames .")
VALUES ( " . $fieldValues . " )";

try_mysql_query($link, $query);
?>

foreach($fields as $field) {
    $words = explode($fieldNameValueSeparator, $field);
    $fieldName = $words[0];
    $fieldValue = $words[1];
    $fieldName = stripslashes($fieldName);
    $fieldValue = stripslashes($fieldValue);
    $fieldName = mysqli_real_escape_string($link, $fieldName);
    $fieldValue = mysqli_real_escape_string($link, $fieldValue);
    if ($fieldName == "") continue;
}

if ($requireEmailActivation == "true") {
    if ($fieldName == "username") $username = $fieldValue;
    if ($fieldName == "email") {
        $email = $fieldValue;
    }

    // Store it

    $fieldNames.= "`$fieldName`,";
    if ($fieldValue == "") $fieldValues.= "NULL,";
    else $fieldValues.= "'$fieldValue',";
}


你确定是正则表达式失败了,而不是PHP失败了吗?你有没有在查询之前尝试回显变量

在纯javascript中我也不知道,但在Jquery中,您可以用以下内容替换正则表达式:

    if ($fieldName == "") continue;
}

要确保轻松修复代码,请将错误信息作为解决方案的起点。 您的具体情况如下:

$var= something@leeds.ac.uk;

if ($var.split("@")[1]!=="leeds.ac.uk")
{
//alert invalid email
}
1-然后回显$query的值,查看其中的内容

2-不要忘记注释代码的剩余部分,以便专注于当前点

3-我发现这行中有错误:

$query = "INSERT INTO accounts (" . $fieldNames .")

通过echo($fieldNames)检查此处的(`)用法value

您在何处使用/存储正则表达式?注意,我知道这并不能防止注入!!!但您在何处使用正则表达式作为文本字符串或函数参数并不明显。抱歉,我现在已经包含了更多信息@revoI,我现在确定是PHP失败了。仍然抛出PHP/mysql错误1064。与h有关输入字段中的数据在传递到我的数据库时遇到问题请尝试注释查询,然后回显$FieldName和$FieldValue,或者尝试打印($FieldName)(如果它们是数组)并检查它们是否包含它们应该包含的内容。
$query = "INSERT INTO accounts (" . $fieldNames .")
$fieldNames  .= "`$fieldName`,";