Solved

Regular Expression for Address field...

Posted on 2006-07-10
7
667 Views
Last Modified: 2006-11-18
I need a regular expression to validate my address field, the condns are...
1. Address field must not start with and empty space (also should not be empty)
2. Address field can contain any alpha numeric characters (Only Capital Letters) and any kind of symbols and it should start with one of the Alphanumeric charcters
3. It can contain any number of spaces in between the words (just like a sentence...)
4. It shud not exceed 200 char.

Thank You,
Steve
0
Comment
Question by:steve_mick972
  • 3
  • 2
  • 2
7 Comments
 
LVL 10

Expert Comment

by:0h4crying0utloud
ID: 17077372

This should work:

<body>

  <input type="text" onblur="checkIt(this.value)">

</body>

<script>

function checkIt(testStr) {
  if( !(testStr.length < 200 && testStr.match(/^[a-zA-Z0-9]/))) {
        alert(" not OK");
      }
      else {
        alert("OK");
      }
}

</script>
0
 
LVL 10

Expert Comment

by:0h4crying0utloud
ID: 17077390
Whoops I missed the caps condition...

ths is a user friendly way of doing it:

<body>

  <input type="text" onblur="this.value=this.value.toUpperCase(); checkIt(this.value);">

</body>

<script>

function checkIt(testStr) {
  if( !(testStr.length < 200 && testStr.match(/^[a-zA-Z0-9]/))) {
        alert(" not OK");
      }
      else {
        alert("OK");
      }
}

</script>
0
 
LVL 1

Author Comment

by:steve_mick972
ID: 17077431
It is not working...
Its not matching any proper address...
I tried:
123 ADFOME DSDFD
DFDSFEG SDFSDLKL & SDFJDLSF
SDFGF%SDF 34343 & SDFDSF
the above strings should result in success...

Stings I dont want:
 DFDSFEFS SDFDSF (space as first char)
""(empty string)
etc.

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 63

Expert Comment

by:Zvonko
ID: 17077671
Here my proposal:



<input type=text name="Address" maxLength="200" onBlur="checkAddr(this)">
<script>
function checkAddr(theField){
  if(!theField.value.match(/^[A-Z0-9][A-Z0-9\%\&\.\ ]+$/)){
    alert("Enter Address.");
    theField.select();
    theField.focus();
    return false;
  }
}
</script>


0
 
LVL 1

Author Comment

by:steve_mick972
ID: 17077726
OK, I donno what is happening, Im testing that regular expression using online tool located at:
http://www.regular-expressions.info/javascriptexample.html
but for some reason Zvonko's test string is also does not result valid addresses...

Could you please tell me whether am I doing anything wroing or I am not supposed to check this reg. expr. there???

Thank You,
Steve
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17079675
Do you remove the first and the last forward slash?
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 17079705
This is what you put into "Regular Expression Tester" form's Regexp field:      
 
^[A-Z0-9][A-Z0-9\%\&\.\ ]+$


0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Wrapper for APPs 9 76
Google Maps API and PHP 25 27
Checking data with javascript 3 19
Is there a way to use PhantomJS on a shared server? 4 15
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now