473,387 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Validation of userinput

rwarren1
What is the best way to validate a users input? I need to validate a "Name" entered in a textbox, as well as an enetered "Score". Could someone please help me with this.
Apr 12 '10 #1

✓ answered by Frinavale

When you created the project that you're working on....did you create it as an MVC ASP.NET project or did you create it as a normal ASP.NET application/website?

I suggest you create a class that you can use to validate your user input with.
For example:
Expand|Select|Wrap|Line Numbers
  1. Class myValidation 
  2.  
  3.   Public Shared Function UserNameIsValid(ByVal userName As String)
  4.     Dim isValid As Boolean =  False
  5.     If (someRequirementsAreMet) Then
  6.       isValid = True
  7.     End If
  8.  
  9.     Return isValid
  10.   End Function
  11.  
  12. '...other functions
  13. End Class
  14.  
Now, when the user tries to submit the data to be saved you can validate the data before you actually save it.

In your page code you would have:
Expand|Select|Wrap|Line Numbers
  1.   'In Button Click event:
  2.   If (myValidation.UserNameIsValid(txt_username.Text) AndAlso ...other validation Then
  3.     'update database or whatever you need to do
  4.   Else
  5.     'indicate to the user that there were errors.
  6.   End If
  7.  
-Frinny

8 2365
debasisdas
8,127 Expert 4TB
you need to write a function for the purpose with all the validation logic implemented.
Apr 12 '10 #2
@debasisdas
Thank you for your reply. Could you give me an idea of how to write the function?What I have is:
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtNewName_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles textNewName.Validating
  2.  
  3.   If Not (txtNewName.Text Like "A-Za-z") Then
  4.     e.Cancel = True
  5.  
  6.     txtNewName.Select(0, txtNewName.Text.Length)
  7.  
  8. MessageBox.Show (txtNewName, "Invalid name")
  9.   End If
  10. End Sub
Apr 12 '10 #3
vb5prgrmr
305 Expert 100+
Wrong forum, you need to be in the VB.NET forum...
Apr 13 '10 #4
Frinavale
9,735 Expert Mod 8TB
Are you developing an ASP.NET MVC web application?
Apr 13 '10 #5
@Frinavale
I was told I need to be in VB.Net forum. I am developing a program in visual studio 2008. Thanks
Apr 13 '10 #6
I'm not sure if this is the correct code to use for validating user input. Would someone correct me please. Thank you.

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtNewName_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles textNewName.Validating
  2.  
  3.   If Not (txtNewName.Text Like "A-Za-z") Then
  4.     e.Cancel = True
  5.  
  6.     txtNewName.Select(0, txtNewName.Text.Length)
  7.  
  8. MessageBox.Show (txtNewName, "Invalid name")
  9.   End If
  10. End Sub
Apr 13 '10 #7
Frinavale
9,735 Expert Mod 8TB
When you created the project that you're working on....did you create it as an MVC ASP.NET project or did you create it as a normal ASP.NET application/website?

I suggest you create a class that you can use to validate your user input with.
For example:
Expand|Select|Wrap|Line Numbers
  1. Class myValidation 
  2.  
  3.   Public Shared Function UserNameIsValid(ByVal userName As String)
  4.     Dim isValid As Boolean =  False
  5.     If (someRequirementsAreMet) Then
  6.       isValid = True
  7.     End If
  8.  
  9.     Return isValid
  10.   End Function
  11.  
  12. '...other functions
  13. End Class
  14.  
Now, when the user tries to submit the data to be saved you can validate the data before you actually save it.

In your page code you would have:
Expand|Select|Wrap|Line Numbers
  1.   'In Button Click event:
  2.   If (myValidation.UserNameIsValid(txt_username.Text) AndAlso ...other validation Then
  3.     'update database or whatever you need to do
  4.   Else
  5.     'indicate to the user that there were errors.
  6.   End If
  7.  
-Frinny
Apr 13 '10 #8
tlhintoq
3,525 Expert 2GB
rWarren1: You have started 4 separate threads for this same topic/issue. Please stop doing that. It makes it hard to coordinate efforts to help you.

[]I was told I need to be in VB.Net forum. I am developing a program in visual studio 2008. [/]

Thanks Visual Studio supports several different programming languages. There are forums here for each language: C-Sharp, Visual Basic, Visual C++ and so on. In the future you need to post your questions in the correct forum if you expect the most experienced people to see them so they can help you.
Apr 13 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Stefan Richter | last post by:
Hi, after coding for days on stupid form validations - Like: strings (min / max length), numbers(min / max value), money(min / max value), postcodes(min / max value), telefon numbers, email...
2
by: wumingshi | last post by:
Hi, When validating an XML instance, sometimes the schema is not enough to expression the validation rules. Additional validation rules may be expressed in an application-specific way. For...
2
by: Phil | last post by:
I wonder how one converts a string into a real regex... if I try something like: var sRegex=userinput.value var sResult = sSomestring.replace(sRegex, "xxx"); the sRegex is interpreted as a...
4
by: Tim Meagher | last post by:
I am trying to use both validation controls and to add submit button attributes, but when I add the button attributes, the javascript fpr the validation controls is no longer created for the page. ...
14
by: Matt | last post by:
I want to know if ASP.NET Web Forms Validation Controls are Server-Side or Client-Side form validation? Since I think each validator control can select either 1) JavaScript based error dialog or 2)...
6
by: Stephen | last post by:
Hi, the validation controls dont work on Netscape or Mozilla and only on Internet Explorer why? How do i correct this problem? Thanks
7
by: Ryan Ternier | last post by:
We're running a site that has required field validation on the login page. It works fine on our development / test machines. However, when I upload this site to our live server i get this error. ...
5
by: Chris | last post by:
Based upon some prevoius postings on what to do for adding a 'add' row to a datagrid I utilize the footer to create the 'add' row. The only issue is that I have it sharing the 'UpDate_Command' and...
10
by: JackM | last post by:
I'm still working on validating the phone numbers that are entered on a form but have come across a problem I don't understand how to fix. I can handle most instances when it's in regular US...
3
by: davidjcampos | last post by:
I'm a definite newbie to javascript, but I have put together some workable code from several different examples on the web. I am using both "onchange" event handlers as well as an "onsubmit" event...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.