简体   繁体   中英

Server side/client side validation .net

Ok, so I have a kind of weird problem that I need ideas on how to solve.

I have a vb.net web application that points to a sql database. There's a table with a primary key that is an auto-incremented integer.

When a user adds an object to this table, it doesn't currently check to see if the "First Name" and "Last Name" already exist in one of the datarows. The desired addition to the functionality is as follows:

1)When the user submits the form, check the table to see if such a record already exists.

1.1)If the record doesn't exist proceed with the insert.

2)If that record does exist, display a warning to the user that such a record exists

2.1)The warning should have two buttons, "Continue" and "Cancel"

2.1.1)If the user clicks "Continue" go ahead and add the duplicate record

2.1.2)If the user clicks "Cancel" stop the insert.

I'm still relatively new to web development (a little over a year of experience). I am looking for the "correct" way to do this. The aspect of this task that is making it hard for me is that I have to run the query, and then possibly display and alert (javascript probably). I'm not sure how to display an alert in the middle of the server side validation.

Any ideas or comments are appreciated!

Thanks!

If you wouldn't allow insertion of duplicates, you could just create unique index in your database. However, what you can do now is to get the count of records in the database, where firstname and lastname equals to inserted.

In case of normal SQL it would look like SELECT COUNT(recordID) WHERE firstName = @firstName AND lastName = @lastName;

Or it could look even easier with Entity Framework. Anyway, your question was about "displaying alert in the middle of server side validation". Think about it differently. Think about it as about two checks.

Add another control to your input form, an invisible checkbox near the Submit button. It should contain the expression about user's agreement to insert duplicate record. Once you detect, that record is duplicate, interrupt the validation, and make checkbox visible, but Submit button - disabled. When user checks the checkbox, Submit button should become visible again.

Now, since you are going through the same validation again, you have to take your checkbox into equation - if it is visible and checked, you don't have to check for record duplication anymore, and just submit the record. If you need to re-use that input form, don't forget to uncheck the checkbox and make it invisible once again.

What you want to do here is add a confirm parameter or something like that to your method, like this:

' This is just pseudocode; I'm guessing you can translate it to
' whatever web framework you're using
Sub InsertRecord(ByVal name() As String, Optional ByVal confirm As Boolean = False)
  If DuplicateRecord(name) And Not confirm
    ' Here's where you would render the page with a confirmation dialog
    RenderViewToRequestConfirmation()
    Return
  End

  DoInsertRecord(name)
  RenderViewAfterRecordInserted()
End

Then under normal circumstances, from your front end you would submit a request that would call this method without the confirm parameter. In the case of duplicate records, the server would render a response with a dialog requesting confirmation. If the user clicked 'Yes' (or whatever), then the front end would send the same request but this time with the necessary request params to set confirm to True .

In terms of web requests, this process might look like:

| Request Data                             | Response               |
|------------------------------------------|------------------------|
| { "name": "M Webster" }                  | Page w/ confirm dialog |
| { "name": "M Webster", "confirm": true } | Success page           |

The route that I went is a little confusing... even now that I have it working, but i'll explain it here in case it makes sense to someone else who can better explain it later.

I wrote a function in the code behind that calls the function that checks the database for a duplicate record, but I added these two lines of code above the function declaration:

<System.Web.Services.WebMethod()>
<System.Web.Script.Services.ScriptMethod()>

Then, I wrote a JavaScript function that grabs the value from the Textbox and passes the value to that function in the code behind. This is made possible by those two lines above the function declaration in the code behind and by using the PageMethods object. The actual call from the JavaScript would look like this:

PageMethods.FunctionName(parameter, function(returnValueFromOtherFunction){ 
    ....function stuff
});

Then I assigned the JavaScript function to the onBlur event in the Textbox.

Thanks for the help guys, but I think this is the best way to solve my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM