Solved

validation field for ic and telephone

Posted on 2001-08-31
3
969 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

746 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

14 Experts available now in Live!

Get 1:1 Help Now