ASP.Net word count with a custom validator - asp.net

A requirement for an ASP.Net 2.0 project I'm working on limits a certain field to a max of 10 words (not characters). I'm currently using a CustomValidator control with the following ServerValidate method:
Protected Sub TenWordsTextBoxValidator_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles TenWordsTextBoxValidator.ServerValidate
'' 10 words
args.IsValid = args.Value.Split(" ").Length <= 10
End Sub
Does anyone have a more thorough/accurate method of getting a word count?

You can use one of the builtin validators with a regex that counts the words.
I'm a little rusty with regex so go easy on me:
(\b.*\b){0,10}

This regex seems to be working great:
"^(\b\S+\b\s*){0,10}$"
Update: the above had a few flaws so I ended up using this RegEx:
[\s\x21-\x2F\x3A-\x40\x5B-\x60\x7B-\xBF]+
I split() the string on that regex and use the length of the resulting array to get the correct word count.

I voted for mharen's answer, and commented on it as well, but since the comments are hidden by default let me explain it again:
The reason you would want to use the regex validator rather than the custom validator is that the regex validator will also automatically validate the regex client-side using javascript, if it's available. If they pass validation it's no big deal, but every time someone fails the client-side validation you save your server from doing a postback.

Related

Using proper syntax in expression builder

Am new to Access.
Have a field on a form called Surname.
I would like to add an expression to the AfterChange event for this field that would change its field to say uppercase.
What should the expression say? (once I get the syntax correct I will then be able to work out how to build it).
thanks
You would be able to do this in VBA quiet easily I would guess. Not sure if Expression builder would be able to do that for you. With VBA you will use,
Private Sub yourControlName_AfterUpdate()
Me.yourControlName = UCase(Me.yourControlName)
End Sub
If you are new to VBA, then check out how to write your very first VBA code : www.baldyweb.com/FirstVBA.htm
Hope this helps !

A Minor, but annoying niggle - Why does ASP.Net set SQL Server Guids to lowercase?

I'm doing some client-side stuff with Javascript/JQuery with .Net controls which expose their GUID/UniqueIdentifier IDs on the front end to allow them to be manipulated. During debugging something is driving me crazy: The GUIDs in the db are stored in uppercase, however by the time they make it to the front end they're in lowercase.
This means I can't quickly copy and paste IDs into the browser's console to execute JS on the fly when devving/debugging. I have found a just-about-workable way of doing this but I was wondering if anyone knew why this behaviour is the case and whether there is any way of forcing GUIDs to stay uppercase.
According to MSDN docs the Guid.ToString() method will produce lowercase string.
As to why it does that - apparently RFC 4122 states it should be this way.
The hexadecimal values "a" through "f" are output as lower case characters and are case insensitive on input.
Also check this question on SO - net-guid-uppercase-string-format.
So the best thing you can do is to call ToUpper() on your GUID strings, and add extension method as showed in the other answer.
If you're using an Eval template, then I'd see if you can do this via an Extension method.
something like
public static string ToUpperString(this Guid guid, string format = "")
{
string output = guid.ToString(format);
return output.ToUpper();
}
And then in your Eval block,
myGuid.ToUpperString("B")
Or however you need it to look.
I'm on my Mac at the moment so I can't test that, but it should work if you've got the right .Net version.

Regular Expression for percents (with % sign) in ASP.Net RegEx Validator

I need a regex for the ASP.Net (4) Regex Validation control. It needs to be a RegEx validator to support other dynamic behaviors outside the scope of this post..
I was using the following, but it fails if the user enters the % sign following the number (which is a req of my spec):
^(100(?:\.0{1,2})?|0*?\.\d{1,2}|\d{1,2}(?:\.\d{1,2})?)$
I tried adding an atomic group of ^(?>%?) at the end, with no luck, after reading the excellent post
Regular expression greedy match not working as expected
Does anyone have any ideas?
Try this
^(100(?:.0{1,2})?%?|0*?.\d{1,2}%?|\d{1,2}(?:.\d{1,2})?%?)$
try this one instead:
^0*(100(\.00?)?|[0-9]?[0-9](\.[0-9][0-9]?)?)%?$

HTTPModule filter questions

I have one issues I'm struggling with with regards to my HTTPModule filter:
1) I notice that the module gets it's data in chunks. This is problematic for me because I'm using a regex to find and replace. If I get a partial match in one chunk and the rest of the match in the second, it will not work. Is there any way to get the entire response before I do my thing to it? I have seen code where it appends data to a string builder until it uses a matches on an "" end tag but my code must work for more that just (xml, custom tags, etc). I don't know how to detect the End Of Stream or if that is even possible.
I am attaching the filter in the BeginRequest.
Have a look at this example. It looks for "" in the stream of the page.
Here's a sample project which performs buffered search and replace in an HttpModule using a Request.Filter and Response.Filter. You should be able to adapt this technique to perform a Regex easily.
https://github.com/snives/HttpModuleRewrite

ASP.Net - validating email address with regexp?

When validating an email address with the regex validation component, an additional RequiredFieldValidator must be added to ensure there is a value present.
I've mostly taken care of this with a CustomFieldValidator, and taking care of this with Javascript.
Is there a better way of doing this?
Why wouldn't you just use the RegularExpressionValidator and the RequiredFieldValidator?
If you use a CustomFieldValidator you will need to implement a javascript check and a server side check as well. Using the other two validation controls together need no extra implementation except for a couple of attributes being set and it is the expected way of doing this type of validation with WebForms.
Consider the next programmer that is going to come along and see your different setup and wonder why you went to all this extra work when none of it was needed.
If you fancy doing it in the background code you could use the following function:
Function checkEMail(ByVal email As String) As Boolean
Dim pattern As String = "^((?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|""((?=[\x01-\x7f])[^""\\]|\\[\x01-\x7f])*""\x20*)*(?<angle><))?((?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|""((?=[\x01-\x7f])[^""\\]|\\[\x01-\x7f])*"")#(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angle)>)$"
Dim emailCheck As Match = Regex.Match(email, pattern)
If emailCheck.Success Then
checkEMail = True
Else
checkEMail = False
End If
Return checkEMail
End Function

Resources