1

I have a textbox into which I only want alphanumeric characters and some punctuation; the JSFiddle is here and the JavaScript implementation looks like this:

var TheCleanString = TheInput.replace(/(^\s+|[^a-zA-Z0-9 \\s\(\)\.\-]+)/g, '');

As you can see, this only allows for alphanumeric characters, removes the leading whitespace and allows for parenthesis, dots and hyphens.

Now I want to use the SAME regex to validate that the string that's coming from the client passed this regex. I have something like this:

public bool MadeIt(TheCandidateString) 
{
   if (TheCandidateString passed this
       regex /(^\s+|[^a-zA-Z0-9 \\s\(\)\.\-]+)/g 
       then return true)
}

What's the best way to do this?

1
  • If you're using asp.net look at the RegEx validator - that'll check it on both sides Commented Jan 24, 2014 at 11:52

1 Answer 1

2
public bool MadeIt(string TheCandidateString) 
{
    string regex= @"yourRegex";
    var match = Regex.Match(TheCandidateString, regex, RegexOptions.IgnoreCase);
    return match.Success;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.