How do I validate a zip code in visual basic? - asp.net

I am supposed to use the regularExpressionValidator to verify a ZIP code for a basic webpage I'm making. If the Zip code is valid, the submit button's click event procedure should display the message "Your ZIP code is" followed by the ZIP code and a period.
I don't know how to do an "if" statement to check to see if the zip is valid or not
**Why does the value = 0 when I enter 60611-3456

...don't know how to do an "if" statement...
You were assigned to use a RegularExpressionValidator, and this sounds like homework. If so, it also sounds like the purpose of the assignment is to make this happen without writing any if statements at all.
The validator controls have a feature where a postback event will not occur if validation fails. You use a correct regular expression with a correctly configured validator control, and the code that shows the "Your zip code is..." message will never run. Configuring the validator control is the point of the assignment; you need to do that part on your own. But finding an acceptable regular expression is a distraction from the real learning, and so I don't mind just giving that to you:
^\d{5}(-\d{4})?$

The issue is that your regular expression indicates the four digits must exist if you have the dash. Generally that would be okay but since you're using an input mask the dash always exists, even when it's only five digits. Try the following expression.
ValidationExpression="\d{5}-?(\d{4})?$"
Hope it helps.

Related

Regular Expression Extractor failing in JMeter for asp.net web forms

Using JMeter, I'm attempting to log in to an asp.net web forms application. I recorded the login sequence to a *.jmx file, and now I'm attempting to extract the __VIEWSTATE, __VIEWSTATEGENERATOR and __EVENTVALIDATION hidden inputs using the Regular Expression Extractor post-processor.
In all 3 cases JMeter is extracting the name of the variable that I want to extract into (eg "${viewstate}") instead of the value I want to extract. Here is what the RequestBody looks like when I look at the ViewResults Tree and select "Text":
ReturnUrl=%2F&__VIEWSTATEGENERATOR=%24%7Bviewstategenerator%7D&__EVENTARGUMENT=&__VIEWSTATE=%24%7Bviewstate%7D&ctl00%24ContentPlaceHolder1%24Login1%24LoginButton.x=25&ctl00%24ContentPlaceHolder1%24Login1%24Password=MyPassword%21&ctl00%24ContentPlaceHolder1%24Login1%24LoginButton.y=4&__LASTFOCUS=&ctl00%24ContentPlaceHolder1%24Login1%24UserName=MyUserName&__EVENTTARGET=&__EVENTVALIDATION=%24%7Beventvalidation%7D
Oddly enough, if I select "RegExpTester" in the ViewResults Tree and test my regular expressions, all of them appear to work.
For example, here is what my __VIEWSTATE extractor looks like:
The Regular Expression is this bit of text:
name="__VIEWSTATE" id="__VIEWSTATE" value="(.+?)"
When I enter that expression into the RegExp Tester it finds it. The other 2 also work:
This is my first time using JMeter, I suspect I've got something in the wrong place.
Here is how my HTTP Request is set up:
Here is how the entire project looks:
Where do you expect these values to come from? You're missing one GET request which will open the login page, your test should not start from POST request.
Once you execute GET request - your Regular Expression extractors will capture viewstate and friends and you will be able to log in.
Also consider switching to CSS Selector Extractors as using regular expressions to parse HTML is not the best idea.
The relevant CSS Selector expression would be as simple as input[id=__VIEWSTATE], use value as the attribute. Similarly correlate remaining dynamic values. See ASP.NET Login Testing with JMeter article for more details if needed.

Regex not working validation control

I am new to Regex, I have a Regex expression in an asp.net ExpressionValidator with this expression in it to validate an email address.
The problem is it won't validate the email when the us.af.mil is in capital letters. The error I get is:
unexpected qualifier
All the other times all it recognizes is the lower case part of the email. I have tried several other variations on the code and nothing works. I am sure I am leaving something out but not sure what. Listed below are some of the variations. Hope someone can tell me what I am leaving out.
\w+([-+.']\w+)*#(?i:(us.af.mil))
\w+([-+.']\w+)*#(us.af.mil)|(US.AF.MIL)
\w+([-+.']\w+)*#uUsS.aAfF.mMiIlL
\w+([-+.']\w+)*#(?i)(us.af.mil)

Email validation in asp net web form

Just curious to know, I am using TextMode="Email" in asp net web form which works as expected however when we provide "info" as a part of first word(Before #) like "info#xyz.com" or "jyo.info#xyz.com" or "jyoinfo#xyz.com" then It says invalid email. I believe it is because the first word(Before #) contains "info"(all small) but making any single or all character of "info" in capital letter work fine. Anyone please explain the reason for this behavior and if i am somewhere wrong then please guide me in right direction.
You can specify your own regular expression to make the email test non-case sensitive. Below is a pretty decent regex for email testing (not too aggressive).
^[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$

ASP.Net - Function output shown before function called

I have the following line of code in ASP.Net (VB)
Response.Write("<td class=""tblRow""><strong>" & ITServiceRow.NAME & " </strong><br>" & funcRAGColour(ITServiceRow.RAGSTATUS) & Environment.NewLine)
This should output the Name from ITServiceRow.NAME followed by the result of the function funcRAGColour.
However this is not the case. ASP.Net is outputting the value of the function funcRAGColour. first followed by the value of ITServiceRow.NAME.
Just trying to understand why this might be happening? If I replace the function with static text it executes fine, but when I put the function in it outputs the function result immediately before the name.
The image here, in yellow shows the full output that comes from the function, it is shown before everything else?
Am I missing something obvious here?
Try using String.Format instead to guarantee placement.
Response.Write(string.Format("<td class=""tblRow""><strong>{0}</strong><br />{1}{2}</td>",funcRAGColour(ITServiceRow.RAGSTATUS),Environment.NewLine))
Always do whatever you can to avoid string concatenation. String concatenation is tough on a system and uses much more memory and resources to be garbage collected than you think because it's actually far more complicated. String.Format and StringBuilder help get around this.
I am very suspect of the function funcRAGColour() itself though and think that is the problem. My guess is the function is not returning the output as a string, but instead is using Response.Write() to output it's result. That would cause it's value to appear first since it is called while the string is being assembled.
Keep in mind, Response.Write is NOT the way to do things in ASP.Net. It was need in classic ASP, but ASP.Net has HtmlTextWriters that can be used during the rendering process, controls for result placement, etc.. It's the old school, non object-oriented way of doing things that can get into trouble.

jQuery autocomplete in ASP.NET webforms?

Has anyone used jQuery to populate an autocomplete list on a textbox using ASP.NET webforms? If so, can anyone recommend a good method? From my reading so far, it seems like most people are using delimited lists rather than JSON to bring the items back. I'm open to any ideas that will get me up and running rather quickly.
I made a tutorial to do this with asp.net mvc but it should be almost identical for traditional webforms:
http://blogs.msdn.com/joecar/archive/2009/01/08/autocomplete-with-asp-net-mvc-and-jquery.aspx
There are many, many examples on the web. I've used this one before, and if I recall you only need to create an aspx that will return matching terms as a <BR/> separated list:
http://www.dyve.net/jquery/?autocomplete
The documentation shows php in the example, but there's no difference in the way the plugin itself works and I didn't have to do anything special as a result.
From the documentation:
> $("#input_box").autocomplete("my_autocomplete_backend.php");
In the above example, Autocomplete
expects an input element with the id
"input_box" to exist. When a user
starts typing in the input box, the
autocompleter will request
my_autocomplete_backend.php with a GET
parameter named q that contains the
current value of the input box. Let's
assume that the user has typed
"foo"(without quotes). Autocomplete
will then request
my_autocomplete_backend.php?q=foo.
The backend should output possible
values for the autocompleter, each on
a single line. Output cannot contain
the pipe symbol "|", since that is
considered a separator (more on that
later).
An appropiate simple output would be:
foo
fool
foot
footloose
foo fighters
food fight
I wrote an Asp.Net WebControl and some Asp.Net MVC extension methods wrapping the JQuery UI autocomplete widget.
I wrote documentation as well about how to implement a working resource providing a JSon result.
You can find it at:
http://autocompletedotnet.codeplex.com/
Hope it can help

Resources