PDA

View Full Version : RegEx verification help



George Lucas
Nov 29th, 2009, 04:25 AM
i'm trying to make a validation page and i am having issues with some of them

AGE - i'm not sure where to go from here

var age = /^[0-9 ]{3,20}$/; // trying to check age (only 18-25 is valid)

Name - how do i make it so that no spaces are allowed?

/^[A-Za-z ]{3,50}$/

street - i have no clue where to even begin on this. i think that RegEx can't be used to validate all addresses, but something simple like 123 main street. maybe something like this
preg_match('/.{2,60}$/',$address)


here's an example of my zip code one to give you an idea of what im going for


function checkZip(){
var re5digit=/^\d{5}$/ //regular expression defining a 5 digit number
if (document.myform.zip.value.search(re5digit)==-1) //if match failed
alert("Please enter a valid 5 digit number inside form")
}


HTML FORM

<form name="myform" onsubmit="return checkZip()">
<fieldset>
<legend>Test Form</legend>

Firstname*: <input type="text" name="firstname"/><br/>
Lastname*: <input type="text" name="lastname"/><br/>
Street*: <input type="text" name="street"/><br/>
City*: <input type="text" name="city"/><br/>
State*: <input type="text" name="state"/><br/>
Zip*: <input type="text" name="zip"/><br/>
Age*: <input type="text" name="age"/><br/>
Gender*: <input type="text" name="gender"/><br/>
Major/Program Choice*: <input type="text" name="major"/><br/><br/>
<input type="reset" />
<input type="submit" />


what is a good way to combine these into all one function using getElementByID? i could make another function and put all the other functions in it, but i'm not sure where i can incorporate getElementByID

when the verification is complete, i want to display all the info using getElementByID and innerHTML. should i make vars that hold all the info and put in after a span somewhere?

Philip M
Nov 29th, 2009, 10:49 AM
Looking at your previous posts I am bound to ask - Is this a student's assignment?

If you can assure me that it is not, I will be glad to supply the validation codes you need. But if it is homework you need to make a bigger effort yourself and revise/review your course material. This forum is not supposed to be a student's crib sheet.

var age = /^[0-9 ]{3,20}$/; // trying to check age (only 18-25 is valid)
This will validate that the age (in years) consists of at least 3 digits and not more than 20 digits :eek:

To check that age is within a certain range:-


var a = parseInt(document.myform.age.value);
if (a < 18) || (a > 25) {
alert ("Only ages 18-25 can be accepted");
return false;

Note that in Internet Explorer, names and IDs are global variables and thus you should NEVER use a global variable or function name which is the same as an HTML element name or ID.


“A man ceases to be a beginner in any given science and becomes a master in that science when he has learned that he is going to be a beginner all his life.” Robin G. Collingwood (English Philosopher, 1889-1943)