RegEx verification help

Discussion in 'JavaScript' started by George Lucas, Nov 28, 2009.

  1. #1
    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)
    Code (markup):
    Name - how do i make it so that no spaces are allowed?
    /^[A-Za-z ]{3,50}$/
    Code (markup):
    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)
    Code (markup):

    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")
    }
    
    Code (markup):
    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" /> 
    
    Code (markup):
    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?
     
    Last edited: Nov 28, 2009
    George Lucas, Nov 28, 2009 IP
  2. myst_dg

    myst_dg Active Member

    Messages:
    224
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Age - Numbers between 18 and 25:
    ^1[89]|2[0-5]$
    Code (markup):
    Name - What do you mean by no spaces are allowed? I'm confused... Anyway, the regex for letters only string, and starts with caps:
    ^[A-Z][a-z]+$
    Code (markup):
    This one can also match names like "McDonald", "O'Neal" or so:
    ^[A-Z]'?[A-Za-z]+$
    Code (markup):
    Street - I have no idea because there are no standard formats in street names. Maybe this one is a better choice, just to ensure the field is not left blank:
    ^.+$
    Code (markup):
    or according to your requirement, number followed with letters:
    ^\d+\w+$
    Code (markup):
    Zipcode - you've solved this
    ^\d{5}$
    Code (markup):
    You may need to give a unique ID to each field you want to validate
    For example the age:
    var age=document.getElementById('age');
    var ageRegex=/^1[89]|2[0-5]$/;
    if (!age.match(ageRegex)) {
        //invalid message, blah blah blah...
    }
    Code (markup):
     
    myst_dg, Dec 2, 2009 IP