Solved

validation field for ic and telephone

Posted on 2001-08-31
3
985 Views
Last Modified: 2006-11-17
hai, i need javascript solution in validation fields

1.
<INPUT type="text" NAME="ic1" SIZE="6">-<INPUT type="text" NAME="ic2" SIZE="2">-
<INPUT type="text" NAME="ic3" SIZE="4">

the format is like this:
800101-10-5555

ic1 must be 6 digits, ic2 is from 01 to 13(2 digits) , ic3 must also be 4 digits.

2.
also for telephone validation:
<INPUT TYPE="Text" NAME="phone1" SIZE="3">-<INPUT TYPE="Text" NAME="phone2" SIZE="7">
the format is 089-123456 or 010-1234567
0
Comment
Question by:J2J
  • 2
3 Comments
 
LVL 2

Expert Comment

by:psogaa
ID: 6444604
Something like this?( added "maxlength" properties in the input boxes for convenience ).

<html>
<head>
<script language=javascript>
function validateForm( myForm ){
  var ic1Reg = /^\d{6}$/;
  var ic2Reg = /^\d{2}$/;
  var ic3Reg = /^\d{4}$/;
  if( !ic1Reg.test( myForm.ic1.value ) || !ic2Reg.test(myForm.ic2.value) || !ic3Reg.test(myForm.ic3.value)){    
    alert('Incorrect ic format');
    return false;
  }
  else if( myForm.ic2.value > 13 ){
    alert('Ic2 value must be less than 14');
    return false;
  }
  return true;
}
</script>
</head>
<body>
<form onsubmit="return validateForm(this);">
<INPUT type="text" NAME="ic1" SIZE="6" maxlength=6>-<INPUT type="text" NAME="ic2" SIZE="2" maxlength=2>-
<INPUT type="text" NAME="ic3" SIZE="4" maxlength=4><br><br>
<input type=submit value=submit>
</form>
</body>

0
 
LVL 2

Accepted Solution

by:
psogaa earned 30 total points
ID: 6444623
oops, missed the phone thing.
Here goes :)

<html>
<head>
<script language=javascript>
function validateForm( myForm ){
  var ic1Reg = /^\d{6}$/;
  var ic2Reg = /^\d{2}$/;
  var ic3Reg = /^\d{4}$/;
  var phone1Reg = /^\d{3}$/;
  var phone2Reg = /^\d{6,7}$/;
  if( !ic1Reg.test( myForm.ic1.value ) || !ic2Reg.test(myForm.ic2.value) || !ic3Reg.test(myForm.ic3.value)){    
    alert('Incorrect ic format');
    return false;
  }
  else if( myForm.ic2.value > 13 ){
    alert('Ic2 value must be less than 14');
    return false;
  }
  if( !phone1Reg.test(myForm.phone1.value) || !phone2Reg.test(myForm.phone2.value) ){
    alert('Incorrect phone format');
    return false;
  }
  return true;
}
</script>
</head>
<body>
<form onsubmit="return validateForm(this);">
<INPUT type="text" NAME="ic1" SIZE="6" maxlength=6>-<INPUT type="text" NAME="ic2" SIZE="2" maxlength=2>-
<INPUT type="text" NAME="ic3" SIZE="4" maxlength=4><br>
<INPUT TYPE="Text" NAME="phone1" SIZE="3" maxlength=3>-<INPUT TYPE="Text" NAME="phone2" SIZE="7" maxlength=7><br><br>
<input type=submit value=submit>
</form>
</body>
0
 

Author Comment

by:J2J
ID: 6470269
thanks for the solution. bye.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now