how to check value entered in field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhuriks
    New Member
    • Jun 2010
    • 149

    how to check value entered in field

    hi,
    i m new to this site as well as to Javascript.In my project i need to use javascript..in my code if i enter the textfield 'MA' it should take along with numbers...but the problem is if i enter 'MAA' it is not showing any error..the text field should take on 'MA' not 'MAA'...i used the charAt() function...i m not getting any one can help me...its urgent...here i m sending the code..waiting for the reply...

    Code:
    function pasuser(form)
    {
        if (form.n1.value == "")
        {
            alert("Enter Package Name");
            form.n1.focus();
            return (false);
        }
        var chk = form.n1.value;
        var chk1=chk.charAt(0);
        var allValid = true;
        if (chk1=="M")
        {
        	alert("u entered correct");
        	form.n1.focus();
        	allValid = false;
        }
        else if (!allValid)
        {
            alert("Enter only M");
            form.n1.focus();
            return (false);
        }
        form.submit();
        return true;
    }
    Thanks&Regards,
    madhuri.
    Last edited by Dormilich; Jun 11 '10, 10:40 AM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    do you mean that the text field should take only "MA" or MA accompanied with numbers, like "MA12345"?

    Comment

    • madhuriks
      New Member
      • Jun 2010
      • 149

      #3
      how to get checked the field

      hi,
      it should take 'MA123456789'.. .not 'MAA123456789'. .i tried using indexOf() and charAt()...but unable to get it...can u help me out from this....

      Thanks&Regards,
      Madhuri.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        then I recommend that you test your string against a RegEx:
        Code:
        if (/^MA\d+$/.test(document.forms[0].n1.value) {
            alert("valid");
        }

        Comment

        • madhuriks
          New Member
          • Jun 2010
          • 149

          #5
          how to get checked the field

          hi,
          thanks for your reply..i tried i m not getting..if i enter wrong value i.e 'nn'..it is not showing error..here with i m sending the code can u once check it..

          Code:
          function pasuser(form)
          {
              if (form.n1.value == "")
              {
                  alert("Enter Package Name");
                  form.n1.focus();
                  return (false);
              }
              if (/^MA\d+$/.test(document.forms[0].n1.value))
              {
                   alert("valid");
                   form.nl.focus();
                   return (false);
               }
              form.submit();
              return true;
          }
          Thanks & Regards,
          madhuri.
          Last edited by Niheel; Jun 11 '10, 11:32 AM. Reason: Please use code tags to display code.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            this might be an addressing issue (the part before .value), because I don’t know your form setup.

            Comment

            Working...