Custom Validator on phone number and further information on it - asp.net

I'm looking for a good site that explains CustomValidator in detail. If anyone has a go to please let me know. The following code checks that there are at least 10 numbers in the result. However, I'm looking to also have it validate that the values are numbers. Using CustomValidator in vb.net is there a way to do this as an "and if" statement?
Thank you
Sub AtLeastTenNumbers_ServerValidate(ByVal source As Object, _
ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
If area_code.Text.Length + phone_1.Text.Length + phone_2.Text.Length > 9 Then
args.IsValid = True
Else
args.IsValid = False
End If
End Sub

This is a bit of an old article, but Scott Mitchell knows his stuff:
https://web.archive.org/web/20211020145934/https://www.4guysfromrolla.com/articles/073102-1.aspx
is there a way to do this as an "and if" statement?
You can always nest an if statement inside of your current statement.
If area_code.Text.Length + phone_1.Text.Length + phone_2.Text.Length > 9 Then
args.IsValid = True
'Check to see if this part is numeric
If IsNumeric(phone_1.Text) Then
' Do Logic here
End If
Else
args.IsValid = False
End If

Related

Vb.net Simple TextBox conversion to integer gives error input string was not in a correct format

I have some text boxes on a form I would like to validate. I want to confirm that they contain only numbers, and that the "total" text box is equal to the sum of the other boxes:
Protected Sub TotalBoxValidator_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles TotalBoxValidator.ServerValidate
If ((Convert.ToInt32(Box1.Text) + Convert.ToInt32(Box2.Text) + Convert.ToInt32(Box3.Text) + Convert.ToInt32(Box4.Text) + Convert.ToInt32(Box5.Text) + Convert.ToInt32(Box6.Text) + Convert.ToInt32(Box7.Text)) = Convert.ToInt32(TotalBox.Text)) Then
args.IsValid = True
Else
args.IsValid = False
End If
End Sub
Now during testing, I fill out the boxes with only integers. The total box is automatically updated to be the sum of the other boxes. However, when I click my "Submit" button on the form, and all the validators run, this one does not work properly.
The code throws an error on the first if statement, saying that the input to the Convert.ToInt32 calls is invalid. "input string was not in a correct format". I am positive that I am putting integers in each box, and that my TotalBox is equal to the sum of the other boxes. So I'm kind of stuck on why this would throw an error. Perhaps I am doing my conversion from string to integer wrong?
Does anyone have any idea what is going on here?
My issue had to do with the fact that my TotalBox (an ASP.net TextBox control) had its "Enabled" property set to false. I did this because it made the textbox greyed out, and the user could not change the value of the text box. Because the TextBox was not Enabled, I could not access the value of the TextBox at runtime. Though I was successfully changing its value in the page's javascript, and visually the value of the textbox was updating on the page, trying to use its value as a string in the code behind caused the error.
It did not have to do with the logic of the code or any syntax error. To fix this problem, I set the Enabled property of the TotalBox to true. Now the box is no longer greyed out, and user has the ability to change their total to be different than the sum of the boxes, which is not exactly what I want, but then again I am validating the total here so it is not a big deal. I will go back and try to grey out the textbox in javascript instead, because I have a feeling this will maintain the ability to get the TextBoxes value, while still having it be greyed out.
Edit: The above solution did work. For others experiencing this problem, consider trying to grey out the textbox using javascript, instead of any .net or asp code.
Here is the updated code:
Protected Sub TotalBoxValidator_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles TotalBoxValidator.ServerValidate
Dim Box1Value = Convert.ToInt32(Box1.Text)
Dim Box2Value = Convert.ToInt32(Box2.Text)
Dim Box3Value = Convert.ToInt32(Box3.Text)
Dim Box4Value = Convert.ToInt32(Box4.Text)
Dim Box5Value = Convert.ToInt32(Box5.Text)
Dim Box6Value = Convert.ToInt32(Box6.Text)
Dim Box7Value = Convert.ToInt32(Box7.Text)
Dim TotalBoxValue = Convert.ToInt32(TotalBox.Text)
If (Box1Value + Box2Value + Box3Value + Box4Value + Box5Value + Box6Value + Box7Value = TotalBoxValue) Then
args.IsValid = True
Else
args.IsValid = False
End If
End Sub
Now, here is what was really helpful for debugging this situation. Try putting your conversions on different lines, because then when the program halts, it shows you exactly which conversion is going wrong. In my case, I got an error on the TotalBoxValue line. So I knew that something was going wrong with my TotalBox. What was different with the TotalBox than any of the other text boxes? Well, first of all, it was the total. But I knew this was working correctly. So then I thought about its properties. Its enabled property was set to false, different from the other boxes. So i knew this had to be changed.
For some reason in asp.net, when a control has its Enabled property set to false, you cannot access any of its properties at runtime. This is something I have noticed that is maybe not intuitive.
I suggest you use CompareValidators on your textboxes.
This will assure that the page won't even postback if your values aren't appropriate. Example markup:
<asp:CompareValidator ErrorMessage="errormessage" ControlToValidate="Box1"
runat="server" Type="Integer" />
Then you could technically keep the code you've already posted, though if you wanted to forego the validators something like this would treat invalid input as 0:
Dim temp As Integer
Dim boxTotal = {box1.Text, _
box2.Text, _
box3.Text, _
box4.Text, _
box5.Text, _
box6.Text, _
box7.Text} _
.Select(Function(text) If(Integer.TryParse(text, temp), temp, 0)) _
.Sum()
Integer.TryParse(TotalBox.Text, temp)
args.IsValid = boxTotal = temp
You are attempting to convert a text value to an integer before knowing if the text value is an integer, please read up on
integer.TryParse
dim v1, v2, v3, v4, v5, v6, v7 as integer
if not integer.tryparse(box1.text, v1) then
args.IsValid = False
return
endif
if not integer.tryparse(box2.text, v2) then
args.IsValid = False
return
endif
......

ASP.Net - Reducing repetitive code for validation - VB

I have a form with many drop down list boxes on. Each of which I am showing or hiding a row of a table based on its value then adding a requiredfieldvalidator to the text box contained in that row. I am doing this on the selectedindexchanged event of each drop down list, the code for which can be seen below:
Protected Sub cbOffCover_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbOffCover.SelectedIndexChanged
If cbOffCover.SelectedValue = "N" Then
OffCoverRow.Visible = True
Dim rfOffcover As RequiredFieldValidator = New RequiredFieldValidator
With rfOffcover
.ControlToValidate = txtOffCover.ID
.SetFocusOnError = True
.ErrorMessage = "*"
.ForeColor = System.Drawing.Color.Red
End With
OffCoverCell.Controls.Add(rfOffcover)
Else
OffCoverRow.Visible = False
Dim c As Control
For Each c In OffCoverCell.Controls
If c.ID = "rfOffCover" Then
OffCoverCell.Controls.Remove(c)
End If
Next c
End If
End Sub
I then reuse this code for each drop down list to show/hide a differently named row and apply validation to a different text box.
My question being is there a better way of doing this? I don't know exactly how but I can't help but think I don't have to write this much code (or copy/paste) over and over again for each drop down list. Is it possible to write a function/class that will do the work and I can just call that instead? Might seem basic but i'm new to asp/vb. Many thanks
You can put it in a function that returns a boolean. When you call the function, pass it the combobox itself and whatever values you want to validate against. If it matches, return true. Try something like this:
Public Function ValidateComboBox(someComboBox as ComboBox, expectedValue as String)
Dim result as Boolean = False
If someComboBox.SelectedValue = expectedValue Then
result = True
OffCoverRow.Visible = True
Dim rfOffcover As RequiredFieldValidator = New RequiredFieldValidator
With rfOffcover
.ControlToValidate = txtOffCover.ID
.SetFocusOnError = True
.ErrorMessage = "*"
.ForeColor = System.Drawing.Color.Red
End With
OffCoverCell.Controls.Add(rfOffcover)
Else
OffCoverRow.Visible = False
Dim c As Control
For Each c In OffCoverCell.Controls
If c.ID = "rfOffCover" Then
OffCoverCell.Controls.Remove(c)
End If
Next c
End If
Return result
End Function
Of course, modify it to fit your needs. Maybe you only return the value, and do the other stuff inside the control's SelectedIndexChanged method.

Retrieving data through LINQ to SQL

I got a weird problem here like I got a site that users can post comments on a friend profile page.Everytime a user post a comment my application sends e-mail to that page owner, you know to inform for a new posted comment on his/her profile page.The problem is I want to stop the application from sending email if that user has just recently posted a comment say like 5 hours ago/earlier.Here is the function I use that would try to check it:
Public Function CheckForNewPost(ByVal arg As String) As Boolean
Dim x As Integer = 0
Using dc As New WhatEverDataContext()
Dim newcomment = From mytable In dc.PostTable _
Where mytable.PostingUser.ToLower() = User.Identity.Name.ToLower() And mytable.PageOwner.ToLower() = arg.ToLower() And mytable.PostedDate.AddHours(5) >= DateTime.Now _
Select mytable
For Each comment In newcomment
x = x + 1
Next
If x > 0 Then
'user has posted a comment recently
Return True
Else
Return False
End If
End Using
End Function
Then I use it like this:
Protected Sub Repeater1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewInsertedEventArgs) Handles Repeater1.ItemInserted
'send our mail
Dim PageOwner As String = Request.QueryString.Get("PageOwnerName")
If CheckForNewPost(PageOwner) = False Then
SendEMail(PageOwner)
End If
End Sub
But still the app still sending the mail even the user just posted 5 hours earlier.
What do you think I'm doing here?
I think it will be clearer if you write your condition as
mytable.PostedDate <= DateTime.Now.AddHours(-5)
as the right hand side now reads as "Five Hours Ago."
So the entire condition now reads as My posted date is earlier than (or equal to) five hours ago.
You should be checking to see if mytable.PostedDate.AddHours(5) <= DateTime.Now. I think you just got it backwards.
mytable.PostedDate.AddHours(5) <= DateTime.Now

Online Test using ASP.NET / Linq

Working on a Online Test.
I have 3 tables
Questions
Subject
Topic
I have made a stored procedure which returns 25 random records. I want to store it in-memory and then display 1 question at a time with AJAX. I don't want to hit database 25 times as there are many users, I tried and store the result in viewstate but then I am not able to cast it back. if I use
Dim qus = from viewstate("questions")
it works, but it doesn't work when I retrieve 1 record at a time.
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
ViewState.Add("QuestionNo", 0)
Dim qus = From q In PML.PM_SelectRandomQuestionFM Select q
viewstate.add("questions",qus)
LoadQuestion(0)
End If
End Sub
Private Sub LoadQuestion(ByVal i As Integer)
Dim QuestionNo As Integer = CType(ViewState("QuestionNo"), Integer) + 1
Try
If QuestionNo <= 25 Then
Dim qus = viewstate("questions")
Me._subjectTopic.Text = String.Format("<b>Subject:</b> {0} -- <b>Topic:</b> {1}", qus(i).subjectName, qus(i).TopicName)
Me._question.Text = " " & qus(i).Question
Me._answer1.Text = " " & qus(i).Answer1
Me._answer2.Text = " " & qus(i).Answer2
Me._answer3.Text = " " & qus(i).Answer3
Me._answer4.Text = " " & qus(i).Answer4
Me._questionNo.Text = String.Format("Question No. {0} / 25", QuestionNo)
ViewState.Add("QuestionNo", QuestionNo)
Else
Server.Transfer("freeMemberResult.aspx")
End If
Catch ex As Exception
Throw New System.Exception(ex.ToString)
End Try
End Sub
I tried casting the object to
Dim qus = CType(ViewState("questions"), IQueryable(Of PM_SelectRandomQuestionFMResult))
but then I get this error
System.Linq.Enumerable+WhereSelectEnumerableIterator`2
Please HELP or if there is any other method to do it, if my method of doing online test is wrong.
Regards
IMO, you're over-engineering this. Why screw around trying to hold the data in memory? Why not write each question to a div, and then hide all of the question divs except for the "current question".
Much easier to implement and you're not hitting the server with several AJAX calls, This also make saving state (previously answered questions, etc) much, much easier.
Have you tried just using Session to maintain state? Is there a requirement that prohibits you from doing this?
Dim qus = CType(Me.Session("questions"), IQueryable(Of PM_SelectRandomQuestionFMResult))

dissable image buttons [duplicate]

I am trying to compare a session variable to another string so that i can enable or disable image buttons. I am using asp.net vb with a sql2005 express backend
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim string1 As String
Dim string2 As String
Label1.Text = Session("Username")
Label2.Text = Session("UserId")
string1 = Session("Username")
string2 = "medev"
If String.Compare(string1, string2, True) = 1 Then
ImageButton1.Enabled = False
End If
String.Compare returns -1, 0 or 1 depending on the result:
String.Compare("A", "B", true) ' returns -1 '
String.Compare("B", "A", true) ' returns 1 '
String.Compare("B", "B", true) ' returns 0 '
In your case, ImageButton1 will be disabled if the user name would come before "medev" if you sorted them as a list. So "Adam" wound have it disabled, while "Stuart" would have it enabled. "medev" would also get it disabled, because if the strings are equal, String.Compare returns 0. My guess is that you want ImageList1 enabled for "medev":
If String.Compare(string1, string2, True) <> 0 Then
ImageButton1.Enabled = False
End If
I would also suggest using the String.Compare overload that takes a StringComparison instead of a bool to get a case insensitive compare. In my opinion it makes it a lot easier to understand what the code does:
If String.Compare(string1, string2, StringComparison.InvariantCultureIgnoreCase) <> 0 Then
ImageButton1.Enabled = False
End If
String Compare returns 0 if the strings are equal. To evaluate for true change your comparison to = 0 rather than <> 0.
If String.Compare(string1, string2, StringComparison.InvariantCultureIgnoreCase) = 0 Then
If you envision performing such checks frequently to display different portions of a page you might want to break your pages up based on the different types (or levels) of users you expect and redirect them to a particular page upon login. Depending on how similar the pages are between users you'll likely find using master pages and/or user controls to be beneficial.
this is works!
ImageButton1.Enabled = Not Session("UserName") Is Nothing AndAlso Session("UserName").ToString.ToLower.Trim.Equals("username")
ImageButton2.Enabled = Not Session("UserName") Is Nothing AndAlso_ Session("UserName").ToString.ToLower.Trim.Equals("username")

Resources