Solved

javascript only a-z A-Z and while space

Posted on 2006-10-24
5
319 Views
Last Modified: 2008-02-26
Hello,

I'm writing a javascript that will validate a form input. only a-z A-Z and while space are
allowed.

var passwdPat=/[^a-zA-Z]\s/g; seems not working well. is there something missing in this pattern?

Thank you so much for your reply,

lilyyan

0
Comment
Question by:lilyyan
  • 3
  • 2
5 Comments
 
LVL 30

Accepted Solution

by:
callrs earned 500 total points
Comment Utility
<script type="text/javascript">
// Allow spaces and letters only
function validate(frm){
var v=frm.n1.value;
var rx=/^[a-z\s]*$/i;
if(!rx.test(v)) {alert("Spaces and letters only"); frm.n1.focus(); return false;}
else alert("valid");
return false;
}

</script>
<form onsubmit="return validate(this)">
<input name=n1 value="abcd123">
<input type=submit value="Submit">
</form>
0
 

Author Comment

by:lilyyan
Comment Utility
hi,

thanks for your reply. it works great!

I'm trying to understand the expression. i'm relative new to exprssion.

/^[a-z\s]*$/i;

i : means ignore the letter case ?

anything between / / is a pattern ?

[a-z\s ] means any input letter could be a-z or space?

* means 0 to any number

$ means ?

Thanks for your reply,

lilyyan
0
 
LVL 30

Expert Comment

by:callrs
Comment Utility
i  - case insensitive
*  -  0 or more
+  -  1 or more
?   - 0 or 1, or it can mean non-greedy, or...
^  -  starts with, or inside square brackets it means NOT, e.g. [^a-z] means match if it not a-z
$  - ends with

http://www.experts-exchange.com/Q_21995615.html  repalce parameters
Getting started with Regular Expressions: http://www.experts-exchange.com/Q_21980291.html
0
 

Author Comment

by:lilyyan
Comment Utility
so /^[a-z\s]*$/i;  means the valid input is any letter (0 or more ) starts with a-z or space
and ends with  a-z or space. is this correct?

thanks for your reply!
0
 
LVL 30

Expert Comment

by:callrs
Comment Utility
It's an expression that checks that a string starts and ends with ONLY 0 or more bytes of a-z A-Z space. If you had 'a1b', the test is false since the first 3 bytes ('a1b') has a 1, and the last 3 bytes (again 'a1b') has a 1.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

744 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

12 Experts available now in Live!

Get 1:1 Help Now