[JAVASCRIPT] Need some help with JS validations

Discussion in 'Web Programming' started by Rahman, Apr 7, 2013.

[JAVASCRIPT] Need some help with JS validations
  1. Unread #1 - Apr 7, 2013 at 8:57 AM
  2. Rahman
    Joined:
    Dec 1, 2011
    Posts:
    1,771
    Referrals:
    0
    Sythe Gold:
    46
    Easter 2013

    Rahman Guru
    $5 USD Donor New

    [JAVASCRIPT] Need some help with JS validations

    1. How would I make a textbox accept only alphacharacters and hyphens?

    2. How do I make a certain textbox a mandatory field if an option has been checked in a previous drop down menu?

    3. How to automatically fill or generate certain text dependant on a prior selection.

    Eg, a drop down menu stating what forum you are from, if Sythe was selected it would auto-fill URL of sythe.

    4. If a certain string is inputted(Australia) into a prior textbox, the following can only accept 4 characters otherwise 9.

    5. If a certain string is inputted(Australia) into a prior textbox, phone number must be 10 digits only, in formal (xx)xxxxxxxx if string doesnt match, number must start with '+' and have minimum of 10 numbers


    If anyone could help me out with this it would really mean alot, willing to pay abit for efforts if you go above and beyond.'


    MSN - [email protected]
    SKype - tonmoii
     
  3. Unread #2 - Apr 7, 2013 at 10:09 PM
  4. Rahman
    Joined:
    Dec 1, 2011
    Posts:
    1,771
    Referrals:
    0
    Sythe Gold:
    46
    Easter 2013

    Rahman Guru
    $5 USD Donor New

    [JAVASCRIPT] Need some help with JS validations

    Bump...
     
  5. Unread #3 - Apr 8, 2013 at 2:48 AM
  6. Aktion
    Joined:
    Apr 8, 2013
    Posts:
    34
    Referrals:
    0
    Sythe Gold:
    0

    Aktion Member

    [JAVASCRIPT] Need some help with JS validations

    Code:
    function checkchars(element, AlertMessage){
        var allowedchars = /^[a-zA-Z\-]+$/;
        if(element.value.match(allowedchars))
        {
        	alert("Successful!");
        	return true;
        }else{
            alert(AlertMessage);
            element.focus();
            return false;
        }
    }
    This is one way of doing #1. If your using a submit button, use onClick="" to call on the checkchars() function. IE:

    Code:
    <input type='button'
        onclick="checkchars(document.getElementById('txtletters'), 'Please enter letters and -'s only!')"
        value='Submit' />
     
  7. Unread #4 - Apr 9, 2013 at 4:21 AM
  8. Rahman
    Joined:
    Dec 1, 2011
    Posts:
    1,771
    Referrals:
    0
    Sythe Gold:
    46
    Easter 2013

    Rahman Guru
    $5 USD Donor New

    [JAVASCRIPT] Need some help with JS validations

    Thanks mate, worked out fine!

    I've figured out 1-3.

    Could anyone help me out with 4 and 5?

    How would I compare the length of a string which contains integers?
     
  9. Unread #5 - Apr 10, 2013 at 3:49 PM
  10. Aktion
    Joined:
    Apr 8, 2013
    Posts:
    34
    Referrals:
    0
    Sythe Gold:
    0

    Aktion Member

    [JAVASCRIPT] Need some help with JS validations

    Code:
    <script type="text/javascript">
    function setlength() {
    	
    	if (document.getElementById('country').value == "Australia") {
    		document.getElementById('othertext').setAttribute("maxlength", 4);
    	} else {
    		document.getElementById('othertext').setAttribute("maxlength", 9);
    	}
    }
    </script>
    
    <input id="country" type="text" style="width:400px;" onkeyup="setlength()"/>
    <input id="othertext" type="text" style="width:400px;" maxlength="9" onkeyup="setlength()"/>
    
    Heres the answer for number 4. Though I would suggest using a dropdown list for the countries because this script IS case sensitive. Either that, or add an or statement to the if statement (||), and use "australia" as well.

    For number 5, I can write you up a script, but using the jQuery .mask element would probably be your best bet, then check the length of the phone number upon submitting. Let me know if jquery seems like a route you would like to take.
     
  11. Unread #6 - Apr 10, 2013 at 6:16 PM
  12. Rahman
    Joined:
    Dec 1, 2011
    Posts:
    1,771
    Referrals:
    0
    Sythe Gold:
    46
    Easter 2013

    Rahman Guru
    $5 USD Donor New

    [JAVASCRIPT] Need some help with JS validations

    Thanks for that.

    I've started number 5 and gotten it to work 50%.

    By that, I mean it works for domestic phone numbers. By checking for charAt 0 and 3 for ( ) and that works fine.

    in my second if statement I have check charAt(0) which it does for + but after I add && variable.length >= 11. Nothing happens, it doesn't even check for the second part or should I try a nested if statement

    And check if it passes first condition, to pass second condition then return true or return false straightaway and ask to meet the first requirement for the '+' first b4 moving on to the length statement
     
< Need SMF Assistance! | Java Script help? >

Users viewing this thread
1 guest


 
 
Adblock breaks this site