17287

Text box validation using regular expression in asp.net

Question:

I have a textbox and have to use the regular expressions in asp.net

My text should not allow the spaces in first and last place.

<h2>Output should be:</h2>

Valid: [India Bangalore]

Not valid: [ India Bangalore ]

i.e : user can enter the spaces in between the words but not in first position and last position.

If you have solution in JavaScript that is also fine.

Answer1:

Try this please :

^[^\s].*[^\s]$

It simply match input which: not to start with any white space ^[^\s] followed by any character even white spaces .* and not end with any white space [^\s]$.

Any way calling Trim() method on input string in server-side is much easy .

Answer2:

Trim() should remove any trailing or leading spaces.

Recommend

  • How to dynamically show/hide rows in a table
  • How to reformat a string
  • how to parse different type of xml data or similar to xml structure
  • how to set “index” : “not_analyzed” globally for elastic search
  • PureCSS scrollable horizontal menu z-index issue
  • save a juptyer notebook with specific name within the code
  • show/hide another combobox, with the help of combobox items
  • Android WebView loading GoogleMap Data Rendering
  • Get XML response value with GDataXML
  • help('modules') crashing? Not sure how to fix
  • SQL Server re-calculate or not?
  • What is the reason that Policy.getPolicy() is considered as it will retain a static reference to the
  • Can XOR be expressed using SKI combinators?
  • Should I use composite primary keys in Grails?
  • (Tcl/Expect) clear screen after exit
  • Use sed with regex and (
  • Scanner nextInt() and hasNextInt() problems
  • Why must we declare a variable name when adding a method to a struct in Golang?
  • jQuery: How to AJAXify WordPress Search?
  • Trying to get the char code of ENTER key
  • Symfony 2. CSRF token is invalid
  • Unable to decode certificate at client new X509Certificate2()
  • Blackberry - Custom EditField Cursor
  • Body moving without any force applied? (Box2d)
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • Excel - Autoshape get it's name from cell (value)
  • Check if a string to interpolate provides expected placeholders
  • Release, debug version and Authorization Google?
  • RestKit - RKRequestDelegate does not exist
  • Is there a mandatory requirement to switch app.yaml?
  • Traverse Array and Display in markup
  • PHP: When would you need the self:: keyword?
  • python regex in pyparsing
  • Android Google Maps API OnLocationChanged only called once
  • Authorize attributes not working in MVC 4
  • Does armcc optimizes non-volatile variables with -O0?
  • Busy indicator not showing up in wpf window [duplicate]
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF