Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 28th, 2008, 11:15 AM
Authorized User
 
Join Date: Aug 2008
Location: , , .
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Regex

Hello All,


I want to validate a text box for email validation using regex. The issue i am facing is:
1) i have 2 textboxes where 1st will carry firstname_lastname
and the 2nd textbox will carry @domain name.

2) I want to validate the first box for firstname_lastname where it should check for the '_' character.

Please help.

  #2 (permalink)  
Old November 28th, 2008, 06:09 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

So do you want to do this client-side in JavaScript or server side in JSP code??? Or, to be safe, do you want to do it both places?
  #3 (permalink)  
Old December 1st, 2008, 03:22 AM
Authorized User
 
Join Date: Aug 2008
Location: , , .
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I want to do this in JavaScript. Thank you

  #4 (permalink)  
Old December 1st, 2008, 04:17 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Code:
<script>
function checkForValidName( fld )
{
    var re = /^[a-z]+\_[a-z]+$/i
    // optional:  Trim spaces from ends first:
    var txt = fld.value.replace( /(^\s+|\s+$)/g, "" );
    // optional:  Put the trimmed value back in place
    fld.value = txt;
    if ( ! re.test( txt ) )
    {
        alert("Must be in form firstname_lastname");
        fld.focus();
        fld.select();
        return false;
    }
    return true;
}
</script>
Then invoke that function via something like
     <form onsubmit="return checkForValidName( this.SomeFieldName );">

Note that this RE will allow names as simple as
    a_b
If you want to insist on (say) a minimum of 3 letters in first name and 2 in second, you could do
    var re = /^[a-z]{3,99}\_[a-z]{2,99}$/i

Or use whatever numbers you feel comfortable with.
  #5 (permalink)  
Old December 8th, 2008, 08:29 AM
Authorized User
 
Join Date: Aug 2008
Location: , , .
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you!



Similar Threads
Thread Thread Starter Forum Replies Last Post
regex mrame XSLT 12 July 25th, 2008 09:37 AM
Regex htran XSLT 2 May 18th, 2005 09:21 AM
More regex help Snib Pro PHP 4 December 16th, 2004 09:56 PM
Need RegEx help Snib Pro PHP 6 November 16th, 2004 06:59 AM
Regex Help boyer99g General .NET 2 October 8th, 2004 05:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.