Results 1 to 9 of 9

Thread: JavaScript form address field validations

  1. #1
    Junior Member
    Join Date
    Mar 2007
    Answers
    2

    JavaScript form address field validations

    In javascript form validations, how to allow numbers, special characters, alphabets in address field? the address field doesn't allow only numbers or alphabets or characters. it allows all the three otherwise it shows the alert message.

    if anybody konw the answer please explain me.


  2. #2
    Contributing Member
    Join Date
    Sep 2006
    Answers
    962

    Re: JavaScript form address field validations

    Hi ashaflint,

    Look at the following code..

    function checkAddress(sText)
    {
    var ValidChars = "0123456789";
    // Here what ever you want to put. As you mentioned numbers or alphabets or characters.
    var IsNumber=true;
    var Char;
    for (i = 0; i < sText.length && IsNumber == true; i++)
    {
    Char = sText.charAt(i);
    if (ValidChars.indexOf(Char) == -1)
    {
    IsNumber = false;
    }
    }
    return IsNumber;
    }

    In the above function sText is the value of the address field. I think it will help to you.

    ------------------------
    suresh


  3. #3
    Contributing Member
    Join Date
    Sep 2006
    Answers
    962

    Re: JavaScript form address field validations

    Hi ashaflint,

    I think it is easy to give the alert message when the user enters only numbers or alphabets or characters...Look at the following script, now i give the alert message when the user enters only numbers.

    function checkAddress(sText)
    {
    var ok = "0123456789";
    var f=true;
    var Char;
    for (i = 0; i < sText.length && f == true; i++)
    {
    Char = sText.charAt(i);
    if (ok.indexOf(Char) == -1)
    {
    f = false;
    }
    }

    if (f==true)
    {
    alert("user enters only numbers");
    }
    }

    So you can change this script to alphabets or characters also. The only thing is you should change the value of "ok". In the above function i give only numbers. I hope this will help to you.

    -----------------------
    suresh


  4. #4
    Contributing Member
    Join Date
    May 2007
    Answers
    32

    Re: JavaScript form address field validations

    hi

    the code given by suresh is correct but little modification then it will work

    function checkAddress(sText)
    {
    var ok = "0123456789";
    var f=true;
    var Char;
    var text=sText.value;
    for (i = 0; i {
    Char=text.charAt(i);
    if(ok.indexOf(Char) == -1)
    {
    f = false;
    }

    }
    if (f==false)
    {
    alert("user enters only numbers");
    }
    }


  5. #5
    Contributing Member
    Join Date
    May 2007
    Answers
    32

    Re: JavaScript form address field validations

    sorry u copy this one

    function checkAddress(sText)
    {
    var ok = "0123456789";
    var f=true;
    var Char;
    var text=sText.value;
    for (i = 0; i {
    Char=text.charAt(i);
    if(ok.indexOf(Char) == -1)
    {
    f = false;
    }

    }
    if (f==false)
    {
    alert("user enters only numbers");
    }
    }


  6. #6
    Contributing Member
    Join Date
    May 2007
    Answers
    32

    Re: JavaScript form address field validations

    function checkAddress(sText)
    {
    var ok = "0123456789";
    var f=true;
    var Char;
    var text=sText.value;
    for (i = 0;i {
    Char=text.charAt(i);
    if(ok.indexOf(Char) == -1)
    {
    f = false;
    }

    }
    if (f==false)
    {
    alert("user enters only numbers");
    }
    }


  7. #7
    Contributing Member
    Join Date
    May 2007
    Answers
    32

    Re: JavaScript form address field validations

    getting problem while displaying for loop

    for (i = 0;i

  8. #8
    Banned
    Join Date
    Oct 2007
    Answers
    5

    Re: JavaScript form address field validations

    function checkAddress(sText)
    {
    var ok = "0123456789";
    var f=true;
    var Char;
    for (var i = 0;i {
    Char=sText.charAt(i);
    if(ok.indexOf(Char) == -1)
    {
    f = false;
    }

    }
    if (f==false)
    {
    alert("user enters only numbers");
    }
    }


  9. #9
    Junior Member
    Join Date
    Apr 2009
    Answers
    4

    Re: JavaScript form address field validations

    in onkeypress function you can check this one like this

    function onlyno(){
    key=key.event();

    if(key.indexOf("0123456789")>-1){
    return true
    else return false
    }

    }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact