question about array

Discussion in 'JavaScript' started by progfrog, Mar 30, 2008.

  1. #1
    hi everyone! need you quickily

    i would like to this: in an email part in a form - i would like to make it validate the adress that it should end in .com and nothing after the .com suffix- so if the user will put in the field - it will give him an invalid notification

    how can i validate that? how can i express in an array the end of an array? is that array.length


    thanx a lot

    progfrog
     
    progfrog, Mar 30, 2008 IP
  2. Morishani

    Morishani Peon

    Messages:
    239
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    For the second question :
    
    <script type="text/javscript">
    function validateEmail(elementValue)
    	{
    	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.com$/;
    	return emailPattern.test(elementValue);
    	}
    </script>
    
    HTML:
    I don't understand what you want in the first question, the last element in the array is generally array.length-1, and to access hem you need to do this :
    
    var a = array("asdf","asdf","asdf");
    ...
    x = a[a.length-1];
    alert(x); // x is the last member in the array 
    
    Code (markup):
    I'm assuming that the array keys are 0,1,...,n .
     
    Morishani, Mar 30, 2008 IP
  3. So1

    So1 Peon

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    use regular expressions in JS.
    or u can get element of array and use substr() method:
     if(arr[element_number_in_arr].substr(-4) == ".com") { /* do something */ } 
    Code (markup):
    BUT this will not work in IE!
    to compel IE work correct use this
    
    var our_email = arr[element_number_in_arr];
    if (our_email.substr(our_email.length-5) == ".com") { /* do something */ }
    
    Code (markup):
    i reccomend to u use regular expression for email like this:
    /^[a-Z0-9_-\.]+@[a-Z0-9_-\.]+\.com$/
    Code (markup):
     
    So1, Mar 30, 2008 IP