2013-11-04 52 views
0

我最後一個問題得到了很好的回答,也希望這個:)。只允許某個字的驗證

我收到一個非常混亂的錯誤。我正在使用WebMatrix,asp.net和剃鬚刀。遵循這些教程。 http://www.asp.net/web-pages/tutorials/introducing-aspnet-web-pages-2

我需要一個驗證,只接受某些單詞。如「商業」或「S7」「科學」。 我可以讓它只接受一個單詞,但是當我輸入另一個regEx語句時,它不接受任何內容。

Validation.RequireField("name", "Name is needed"); 
Validation.RequireField("surname", "Surname is needed"); 
Validation.RequireField("num", "num is needed"); 
Validation.RequireField("add", "add is needed"); 
Validation.RequireField("no", "no is needed"); 
Validation.RequireField("course", "course is needed"); 
Validation.RequireField("grade", "grade is needed"); 

Validation.Add("no", Validator.StringLength(10,10, "Contact number can not be longer or shoter than 10 digits")); 
Validation.Add("num", Validator.StringLength(13,13,"Is number must be 13 digits")); 
Validation.Add("name", Validator.Regex("^[a-zA-Z ]+$", "Invalid format for a name")); 
Validation.Add("grade", Validator.Range(8,12, "Grades must be between 8 and 12")); 
Validation.Add("course", Validator.Regex("S7"+"Comm", "Course not valid")); 
//Validation.Add("course", Validator.Regex("^Comm$", "Course not valid blah")); 

下面是其餘的代碼。感謝提前:)

@{ 

    var db = Database.Open("ProjectTest"); 
    var Name = Request.Form["name"]; 
    var Surname = Request.Form["surname"]; 
    var IDNumber = Request.Form["num"]; 
    var Address = Request.Form["add"]; 
    var ContactNumber = Request.Form["no"]; 
    var Course = Request.Form["course"]; 
    var TechID = Request.Form["techID"]; 
    var grade = Request.Form["grade"]; 

    Validation.RequireField("name", "Name is needed"); 
    Validation.RequireField("surname", "Surname is needed"); 
    Validation.RequireField("num", "num is needed"); 
    Validation.RequireField("add", "add is needed"); 
    Validation.RequireField("no", "no is needed"); 
    Validation.RequireField("course", "course is needed"); 
    Validation.RequireField("grade", "grade is needed"); 

    Validation.Add("no", Validator.StringLength(10,10, "Contact number can not be longer or shoter than 10 digits")); 
    Validation.Add("num", Validator.StringLength(13,13,"Is number must be 13 digits")); 
    Validation.Add("name", Validator.Regex("^[a-zA-Z ]+$", "Invalid format for a name")); 
    Validation.Add("grade", Validator.Range(8,12, "Grades must be between 8 and 12")); 
    Validation.Add("course", Validator.Regex("S7"+"Comm", "Course not valid")); 
    //Validation.Add("course", Validator.Regex("^Comm$", "Course not valid blah")); 

      if (IsPost){ 
       if (Validation.IsValid()) 
       {var insertQuery = "INSERT INTO UserInfo (Name, Surname, IdNum, Address, ContNum, Course, Grade)" + "VALUES (@0, @1, @2, @3 ,@4, @5, @6)"; 
       db.Execute(insertQuery, Name, Surname, IDNumber, Address, ContactNumber, Course, grade); 
       } 
         }  
     else{ 
      ModelState.AddFormError("There are some errors with your submission"); 
     } 
    } 


<!DOCTYPE html> 

<html lang="en"> 
    <head> 
     <meta charset="utf-8" /> 
     <title></title> 
    </head> 
    <body> 
     <a href="/Home">Home</a><br> 
       If you are an Student that wants to register use this form.<br> 
     <form method="post"> 
     <table> 
      <tr> 
      <td>Name:</td><td><input type="text" name="name"></td> 
      </tr> 
      <tr> 
      <td>Surname:</td><td><input type="text" name="surname"></td> 
      </tr> 
      <tr> 
      <td>ID Number:</td><td><input type="text" name="num"><br></td>    
      </tr> 
      <tr> 
      <td>Address:</td><td><input type="text" name="add"><br></td> 
      </tr> 
      <tr> 
      <td>Contact Number:</td><td><input type="text" name="no"><br></td> 
      </tr> 
      <tr> 
      <td>Course:</td><td><input type="text" name="course" value=""></td> 
      </tr> 
      <tr> 
      <td>Grade:</td><td><input type="text" name="grade"></td> 
      </tr> 
      <tr><td><input type="submit" value="Submit"></td> 
      </tr> 
     </table> 
      @Html.ValidationMessage("name")<br> 
      @Html.ValidationMessage("surname")<br> 
      @Html.ValidationMessage("num")<br> 
      @Html.ValidationMessage("add")<br> 
      @Html.ValidationMessage("no")<br> 
      @Html.ValidationMessage("course")<br> 
      @Html.ValidationMessage("grade") 
     </form> 
     <select name="courses"> 
     <option value="1" id="test">S7</option> 
     <option value="2">Commerce</option> 
     </select> 
    </body> 
</html> 

回答

0

對於兩個能在你的代碼,試試這個:

Validation.Add("course", Validator.Regex("^S7$|^Comm$", "Course not valid")); 

如果您需要添加更多的潛在匹配,只需添加它們以管道符:

Validation.Add("course", Validator.Regex("^S7$|^Comm$|^Science$", "Course not valid")); 

儘管您使用的是數據庫,但您應該詢問您的數據是否可以更好地標準化。課程可以存儲在一個表格中並鏈接到,而不是存儲爲文本?如果你正在學習一個教程,這可能超出了你正在工作的範圍,但是你應該牢記未來,因爲它不會以這種方式驗證數據。

有關正則表達式的更多信息,看看這個教程:http://www.regular-expressions.info/tutorial.html

+0

謝謝:d!這很好!我現在對正則表達式有了更好的理解 – user2952328

+0

如果解決了你的問題,你應該接受這個答案。 – Polynomial