简体   繁体   English

我的 javascript 验证刚刚停止工作,我不知道为什么

[英]My javascript validation just stopped working and I can't figure out why

I have been developing this page for a few days for a school project and it has been working perfectly up until about 10 minutes ago.我已经为一个学校项目开发这个页面几天了,直到大约 10 分钟前它一直运行良好。 I didn't change anything except adding an extra javascript validation.除了添加额外的 javascript 验证之外,我没有更改任何内容。 Now when I run my page and click "register" button, it registers with empty fields instead of popping up the appropriate alerts.现在,当我运行我的页面并单击“注册”按钮时,它会注册空字段而不是弹出相应的警报。 Please help!请帮忙!

<?php

    //Connect to database

include("dbconnect.php");


    // SQL insertion if submit button is pressed

if (isset($_POST['Submit'])) {

    $firstname  = $_POST["firstname"];
    $lastname   = $_POST["lastname"];
    $email      = $_POST["email"];
    $username   = $_POST["username"];
    $password1  = $_POST["password1"];
    $class1     = $_POST["class1"];
    $address    = $_POST["address"];
    $creditcard = $_POST["creditcard"];
    $expiry     = $_POST["expiry"];
    $cvv        = $_POST["cvv"];

    $postcode   = $_POST["postcode"];
    $city       = $_POST["city"];
    $state      = $_POST["state"];
    $street     = $_POST["street"];

    $personcon  = $conn;
    $bbsSQL     = "INSERT INTO TBLUSERS (FIRSTNAME, LASTNAME, EMAIL, USERNAME, PASSWORD, CLASS, ADDRESS, CREDITCARD, EXPIRY, CVV, POSTCODE, CITY, STATE, STREET) VALUES ('$firstname', '$lastname', '$email', '$username', '$password1', '$class1', '$address', '$creditcard', '$expiry', '$cvv', '$postcode', '$city', '$state', '$street')";
    $personinfo = oci_parse($personcon, $bbsSQL);
    oci_execute($personinfo);
    oci_free_statement($personinfo);
    oci_close($personcon);


    //Send Email

    $to      = $email;
    $subject = 'Registration Confirmation';
    $message = 'Welcome, ' . $firstname . ' ' . $lastname . '.' . "\r\n" . "\r\n" . 'Username: ' . $username . "\r\n" . "\r\n" . 'Password: ' . $password1 . "\r\n" . "\r\n" . 'Class: ' . $class1 . "\r\n" . "\r\n";
    $headers = 'From: fake@fake.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
    mail($to, $subject, $message, $headers);


    //Pass the message to the login page after registration

    $Message = urlencode("Thank you for your registration!");
    header("Location:index.php?Message=" . $Message);

}

?>

<html>
<head>
<title>User Registration</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript">

function validate()
{
    //First Name
    var fname = document.myForm.firstname.value;
    var Vfname = /^[a-zA-Z]+$/;
    if(!Vfname.test(fname)){
        alert("Your last name can only contain letters.");
        return false;
    } else {
/*      alert("Valid first name!"); */
        }


    //Last Name
    var Lname = document.myForm.lastname.value;
    var VLname = /^[a-zA-Z]+$/;
    if(!VLname.test(Lname)){
        alert("Your last name can only contain letters.");
        return false;
    } else {
/*      alert("Valid last name!"); */
        }


    //Class
    var cl = document.myForm.class1.value;
    var Vcl = /^[a-zA-Z]{4}+[0-9]{3}+$/;
    if (!Vcl.test(cl)){
        alert("Your class must be 4 letters followed by 3 numbers.");
        return false;       
    } else {
/*      alert("Valid class!");   */
    }


    //Credit Card
    var cc = document.myForm.creditcard.value;
    var Vcc = /^[0-9]{10}+$/;
    if (!Vcc.test(cc)) {
        alert("Credit card number must be 10 digits.");
        return false;           
    } else {
/*      alert("Valid Credit Card!");     */     
    }

    //Post Code
    var pc = document.myForm.postcode.value;
    var Vpc = /^[0-9]{4}+$/;
    if (!Vpc.test(pc)) {
        alert("Post code must be 4 numbers.");
        return false;           
    } else {
/*      alert("Valid Post Code!");   */     
    }       


    //Password Confirmation
    var pwd1 = document.myForm.password1.value;
    var pwd2 = document.myForm.password2.value;
    if(pwd1!=pwd2 || pwd1=="" || pwd2==""){
        alert("Password unmatched!");
        return false;
    }
    else
        alert("Password matched!");
}
}

</script>

</head>



<body>

    <div class="author">
    COMP344 Assignment 1 2015 <br>
    Daniel Buskariol <br>
    42446937
    </div>

    <div id="heading"> New Star Public School </div>
    <br>

    <form name="myForm" method="POST" onsubmit="return validate()">
    <table style="width:40%" cellpadding="10" cellspacing="1" align="center">

  <tr>
    <th colspan="4">Enter Registration Details</th>
  </tr>

    <tr>
    <td colspan="4"></td>
    </tr>

  <tr>
    <td>First Name</td>
    <td><input type="text" name="firstname" tabindex="1" /></td>
    <td>Post Code</td>
    <td><input type="text" name="postcode" tabindex="9" /></td>
  </tr>

    <tr>
    <td>Last Name</td>
    <td><input type="text" name="lastname" tabindex="2" /></td>
    <td>Credit Card</td>
    <td><input type="text" name="creditcard" tabindex="10" /></td>
    </tr>

  <tr>
    <td>Email</td>
    <td><input type="text" name="email" tabindex="3" /></td>
    <td>Expiry</td>
    <td><input type="text" name="expiry" tabindex="11" placeholder="MM/YY" /></td>
  </tr>

    <tr>
    <td>Class</td>
    <td><input type="text" name="class1" tabindex="4" /></td>
    <td>CVV</td>
    <td><input type="text" name="cvv" tabindex="12" /></td>
    </tr>

  <tr>
    <td>Street Number</td>
    <td><input type="text" name="address" tabindex="5" /></td>
    <td>Username</td>
    <td><input type="text" name="username" tabindex="13" /></td>
  </tr>

    <tr>
    <td>Street Name</td>
    <td><input type="text" name="street" tabindex="6" /></td>
    <td>Password</td>
    <td><input type="password" name="password1" tabindex="14" /></td>
  </tr>

    <tr>
    <td>Suburb/City</td>
    <td><input type="text" name="city" tabindex="7" /></td>
    <td>Confirm</td>
    <td><input type="password" name="password2" tabindex="15" /></td>
  </tr>

    <tr>
    <td>State</td>
    <td><input type="text" name="state" tabindex="8" /></td>    
    <td align="right"><input type="reset" name="Reset"/> </td>
    <td><input type="submit" name="Submit" value="Register"/></td>
    </tr>

</table>
</form>
    <br>

    <p> Already a member? Click <a href="index.php">here</a> to login. </p>

</body>

</html>

Open up your developer console and it will point you to the error.打开你的开发者控制台,它会指向错误。

You have an extra { because you missed a { after the else.你有一个额外的{因为你在 else 之后错过了一个{ So either add a { or remove the extra } .所以要么添加一个{要么删除额外的}

The regular expression is wrong because you have a + after the {3} and after the {4}.正则表达式是错误的,因为在{3}之后和 {4} 之后有一个+ Get rid of them.摆脱他们。

In a few places, you put a + quantifier in a regex after already providing an explicit count, eg var Vcl = /^[a-zA-Z]{4}+[0-9]{3}+$/;在一些地方,您在已经提供了明确计数后在正则表达式中放置了+量词,例如var Vcl = /^[a-zA-Z]{4}+[0-9]{3}+$/; ( {4}+ and {3}+ ). {4}+{3}+ )。 That's not legal in a regular expression.这在正则表达式中是不合法的。 If you checked your browser console, you'd probably see a SyntaxError when the validation is performed.如果您检查浏览器控制台,您可能会在执行验证时看到SyntaxError

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

相关问题 Javascript链接停止工作,无法弄清原因 - Javascript links stopped working, can't figure out why 我的javascript函数似乎无法正常工作。 我不知道为什么 - My javascript function doesn't seem to be working. I can't figure out why 我不知道我的 JavaScript 函数不起作用 - I can't figure out my JavaScript function is not working 我的简单路由无法正常工作,我不知道为什么 - My simple routing is not working and I can't figure out why 编码新手,我不明白为什么我的 javascript 按钮无法正常工作? - New to coding,I can't figure out why my javascript buttons are not working properly? 我不知道为什么我的JavaScript无法正常工作…我正在将值动态传递给标签 - I can't figure out why my javascript isn't working… I'm dynamically passing values to a tag classList.add已停止我的代码运行。 我不知道为什么。 你是否可以? - classList.add has stopped my code from running. I can't figure out why. Can you? 似乎无法弄清楚为什么我的JavaScript无法在IE和Chrome中运行 - Can't seem to figure out why my javascript isn't working in IE and Chrome 无法弄清楚为什么javascript无法在我的php文件中工作 - Can't figure out why javascript isn't working in my php file 我的 javascript 代码不会将元素推送到我的数组,我不知道为什么? - My javascript code won't push elements to my array and I can't figure out why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM