Conditions in Javascript Calculations - javascript

In my project the var ans3 is calculated through javascript! I just want to built another script which should auto display some text into the Category of Ans3 Field according to the values of the calculated var3 and age.
Sample example criteria:
if ans3 < 5 and age < 18 ----> display: Normal Range,
else if ans3 < 10 and age < 50 -----> display: Increased Range,
else ... display: Out of Limits!
I know that this may be done using conditions but i can't exactly figure it out! any thoughts please would be usefull!
<html>
<head>
</head>
<body>
<style>
#ans, #ans2 {
display:none;
}
</style>
<div id="bodyfatmonitoring">
<form name="fatmonitor" Method="post" id="fatmonitor" enctype="multipart/form-data">
<h2 style="color:#b3b3cc;">Calculator</h2>
<table>
<tr>
<th> </th>
<th> </th>
</tr>
<tr>
<td>
<label for="first">First: *</label>
<input type="text" name="first" size="35" id="first" required autocomplete="off">
</td>
<td>
<label for="fifth">Fifth: *</label>
<input type="text" name="fifth" size="35" id="fifth" autocomplete="off">
</td>
</tr>
<tr>
<td>
<label for="second">Second: *</label>
<input type="text" name="second" size="35" id="second" autocomplete="off">
</td>
<td>
<label for="sixth">Sixth: *</label>
<input type="text" name="sixth" size="35" id="sixth" autocomplete="off">
</td>
</tr>
<tr>
<td>
<label for="third">third: *</label>
<input type="text" name="third" size="35" id="third" autocomplete="off">
</td>
<td>
<label for="seventh">Seventh: *</label>
<input type="text" name="seventh" size="35" id="seventh" autocomplete="off">
</td>
</tr>
<tr>
<td>
<label for="fourth">Fourth: *</label>
<input type="text" name="fourth" size="35" id="fourth" autocomplete="off">
</td>
<td>
<label for="age">Age: *</label>
<input type="text" name="age" size="35" id="age" autocomplete="off">
</td>
</tr>
<input type="text" name="ans" size="35" id="ans"/>
<input type="text" name="ans2" size="35" id="ans2"/>
<tr>
<td>
<label for="firstname2">Name | Lastname: *</label>
<input type="text" name="firstname2" size="35" id="firstname2" autocomplete="off"/>
</td>
<td>
<label for="mandate">Date: *</label>
<input type="date" name="mandate" size="35" id="mandate" autocomplete="off"/>
</td>
</tr>
<tr>
<td>
<label for="ans3">Ans3 %: *</label>
<input type="text" name="ans3" size="35" id="ans3" autocomplete="off"/>
</td>
<td>
<label for="category">Category of Ans3: *</label>
<input type="text" name="category" size="35" id="category" autocomplete="off"/>
</td>
</tr>
<tr>
<td>
<button type="button" id="calculator" name="calculator" onclick="Calculate();">Υπολογισμός</button>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
<script>
function Calculate() {
var first = document.getElementById('first').value;
var second = document.getElementById('second').value;
var third = document.getElementById('third').value;
var fourth = document.getElementById('fourth').value;
var fifth = document.getElementById('fifth').value;
var sixth = document.getElementById('sixth').value;
var seventh = document.getElementById('seventh').value;
var ans = document.getElementById('ans').value;
var ans2 = document.getElementById('ans2').value;
document.getElementById('ans').value = parseInt(first) + parseInt(second) + parseInt(third) + parseInt(fourth) + parseInt(fifth) + parseInt(sixth) + parseInt(seventh);
document.getElementById('ans2').value = 1.112 - 0.00043499 * parseInt(document.getElementById('ans').value) + 0.00000055 * Math.pow(parseInt(document.getElementById('ans').value), 2) - 0.00028826 * parseInt(document.getElementById('age').value);
document.getElementById('ans3').value = ((4.95 / document.getElementById('ans2').value) - 4.5) * 100;
}
</script>

Related

How to iterate this.value in javascript

I am trying to compare this.value with Drupal.settings["pis_var" + i].hasOwnProperty(j).
However, I am not able to iterate this.value. It is just comparing with first value not one by one.
Any suggestions would be appreciated.
var controllers = $("[data-drupal]");
var pis = $("[data-pis]");
var i = 0;
var controller = $(controllers[i]);
var inventory = $(pis[i]);
var index = 0;
$(controllers[i]).each(function (index) {
console.log(Drupal.settings["pis_var" + i]);
console.log(this.value);
var j=0;
for (j in Drupal.settings["pis_var" + i])
{
if (Drupal.settings["pis_var" + i].hasOwnProperty(j))
{
console.log(Drupal.settings["pis_var" + i][j] + '--' + i + '--' + j + '--' + this.value);
if (this.value !== Drupal.settings["pis_var" + i][j])
{
console.log(this.value + '--' + Drupal.settings["pis_var" + i][j]);
}
}
}
i++;
});
HTML code ( which is drupal code)
I have derived from the page source.
<table>
<tbody>
<tr>
<td class="label">Experiment #: <span class="form-required" title="This field is required.">*</span></td>
<td><div class="form-item" id="ExExCode-wrapper">
<input type="text" maxlength="8" name="ExExCode" id="ExExCode" size="8" value="201914" data-drupal="pic_code" data-pis="ExExCode_pis" class="form-text required" />
</div>
</td>
</tr>
<tr>
<td class="label">Experiment Name <span class="form-required" title="This field is required.">*</span></td>
<td><div class="form-item" id="edit-ExName-wrapper">
<input type="text" maxlength="60" name="ExName" id="edit-ExName" size="60" value="Seismic sliding law" data-drupal="ExName" data-pis="ExName_pis" class="form-text required" />
</div>
</td>
</tr>
<tr>
<td class="label">Experiment Description <span class="form-required" title="This field is required.">*</span></td>
<td><div class="form-item" id="edit-ExDesc-wrapper">
<input type="text" maxlength="80" name="ExDesc" id="edit-ExDesc" size="80" value="Using passive seismics to determin a glacier sliding law" data-drupal="ExDesc" data-pis="ExDesc_pis" class="form-text required" />
</div>
</td>
</tr>
<tr>
<td class="label">Sponsor</td>
<td><div class="form-item" id="edit-ExSponsor-wrapper">
<input type="text" maxlength="18" name="ExSponsor" id="edit-ExSponsor" size="18" value="UNIV/N/A" data-drupal="ExSponsor" data-pis="ExSponsor_pis" class="form-text" />
</div>
</td>
</tr>
<tr>
<td class="label">Sensors</td>
<td><div class="form-item" id="edit-types-Sensor-wrapper">
<input type="text" maxlength="25" name="types_Sensor" id="edit-types-Sensor" size="25" value="" data-drupal="types_Sensor" data-pis="types_Sensor_pis" class="form-text" />
</div>
</td>
</tr>
<tr>
<td class="label">Recorders</td>
<td><div class="form-item" id="edit-types-Recorder-wrapper">
<input type="text" maxlength="25" name="types_Recorder" id="edit-types-Recorder" size="25" value="" data-drupal="types_Recorder" data-pis="types_Recorder_pis" class="form-text" />
</div>
</td>
</tr>
<tr>
<td class="label">Foreign?</td>
<td><div class="form-radios"><div class="form-item" id="edit-ExDF-N-wrapper">
<label class="option" for="edit-ExDF-N"><input type="radio" id="edit-ExDF-N" name="ExDF" value="N" data-drupal="ExDF" data-pis="ExDF_pis" class="form-radio" /> NO</label>
</div>
<div class="form-item" id="edit-ExDF-Y-wrapper">
<label class="option" for="edit-ExDF-Y"><input type="radio" id="edit-ExDF-Y" name="ExDF" value="Y" data-drupal="ExDF" data-pis="ExDF_pis" class="form-radio" /> Yes</label>
</div>
</div></td>
</tr>
<tr>
<td class="label">Billing Address</td>
<td><div class="form-item" id="edit-ExBilling-wrapper">
<textarea cols="50" rows="10" name="ExBilling" id="edit-ExBilling" data-drupal="ExBilling" data-pis="ExBilling_pis" class="form-textarea resizable "></textarea>
</div>
<div class="textarea-identifier description">CKEditor: the ID for excluding or including this element is <em>passcal:sdb/expt/201914/pisreview.edit-ExBilling</em>.</div></td>
</tr>
Thank you in advance.
You're only iterating over one controller. $(controllers[i]) is evaluated when the .each() starts, so it just uses i=0, it doesn't go to the next controller when you do i++.
You should use
controllers.each(function(i) {
...
})
There's no need for you to increment i yourself.

Reading data from tr with an id inside the tbody

I am trying to read data from input field inside "td" with an id inside the "tbody"
The html structure is like this:
<tbody id="tbody1">
<tr>
<td>
<input type="text" id="bacthNo" class="form-control md-has-value" required="">
</td>
<td>
<input type="text" id="location" class="form-control md-has-value" required="">
</td>
<td>
<input type="text" id="qty" class="form-control md-has-value" required="">
</td>
</tr>
<tr>
<td>
<input type="text" id="bacthNo" class="form-control md-has-value" required="" value="0">
</td>
<td>
<input type="text" id="location" class="form-control md-has-value" required="" value="0">
</td>
<td>
<input type="text" id="qty" class="form-control md-has-value" required="" value="0">
</td>
</tr>
</tbody>
<tbody id="tbody2">
<tr>
<td>
<input type="text" id="bacthNo" class="form-control md-has-value" required="">
</td>
<td>
<input type="text" id="location" class="form-control md-has-value" required="">
</td>
<td>
<input type="text" id="qty" class="form-control md-has-value" required="">
</td>
</tr>
<tr>
<td>
<input type="text" id="bacthNo" class="form-control md-has-value" required="" value="0">
</td>
<td>
<input type="text" id="location" class="form-control md-has-value" required="" value="0">
</td>
<td>
<input type="text" id="qty" class="form-control md-has-value" required="" value="0">
</td>
</tr>
</tbody>
Here the "tbody" id is changing dynamically and tr can be in between 1 to 10 for each "tbody".
What i want is to read all the data of "bacthNo","location" and "qty" for each tbody and push into an array on form submit. I had tried several approach but not able to fetch data from this complicated form.
Kindly Help.
apply class to all of your fields and then access using each function of jquery like this
<tr>
<td>
<input type="text" id="bacthNo" class="bacthNo form-control md-has-value" required="">
</td>
<td>
<input type="text" id="location" class="location form-control md-has-value" required="">
</td>
<td>
<input type="text" id="qty" class="qty form-control md-has-value" required="">
</td>
$('#tbody1').find('tr').each(function () {
var eachtr = $(this);
eachtr.find('.bacthNo').val();
eachtr.find('.location').val();
eachtr.find('.qty').val();
//get your all fields
}
for example see this jsfiddle
$('#tblOne > tbody > tr').each(function() {
alert($(this).find("td:first>input").val());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tblOne">
<tbody id="tbody1">
<tr>
<td>
<input type="text" id="bacthNo" class="form-control md-has-value" required="">
</td>
<td>
<input type="text" id="location" class="form-control md-has-value" required="">
</td>
<td>
<input type="text" id="qty" class="form-control md-has-value" required="">
</td>
</tr>
<tr>
<td>
<input type="text" id="bacthNo" class="form-control md-has-value" required="" value="0">
</td>
<td>
<input type="text" id="location" class="form-control md-has-value" required="" value="0">
</td>
<td>
<input type="text" id="qty" class="form-control md-has-value" required="" value="0">
</td>
</tr>
</tbody>
<tbody id="tbody2">
<tr>
<td>
<input type="text" id="bacthNo" class="form-control md-has-value" required="">
</td>
<td>
<input type="text" id="location" class="form-control md-has-value" required="">
</td>
<td>
<input type="text" id="qty" class="form-control md-has-value" required="">
</td>
</tr>
<tr>
<td>
<input type="text" id="bacthNo" class="form-control md-has-value" required="" value="0">
</td>
<td>
<input type="text" id="location" class="form-control md-has-value" required="" value="0">
</td>
<td>
<input type="text" id="qty" class="form-control md-has-value" required="" value="0">
</td>
</tr>
</tbody>
</table>
something like this:
document.addEventListener("DOMContentLoaded", function (event) {
var _rows = document.querySelectorAll('table tr');
//depending on your markup you could use also:
// var _rows = document.querySelectorAll('tr');
// var _rows = document.querySelectorAll('tbody tr');
//or THE WORST CASE if you cannot really change your html:
//document.querySelectorAll('tbody[id*="tbody"] tr');
_rows.forEach(function (row) {
var _bacthNo = row.querySelector('#bacthNo');
var _location = row.querySelector('#location');
var _qty = row.querySelector('#qty');
console.log(_bacthNo.value)
console.log(_location.value)
console.log(_qty.value)
});
});
<table>
<tbody id="tbody1">
<tr>
<td>
<input type="text" id="bacthNo" class="form-control md-has-value" required="">
</td>
<td>
<input type="text" id="location" class="form-control md-has-value" required="">
</td>
<td>
<input type="text" id="qty" class="form-control md-has-value" required="">
</td>
</tr>
<tr>
<td>
<input type="text" id="bacthNo" class="form-control md-has-value" required="" value="0">
</td>
<td>
<input type="text" id="location" class="form-control md-has-value" required="" value="0">
</td>
<td>
<input type="text" id="qty" class="form-control md-has-value" required="" value="0">
</td>
</tr>
</tbody>
<tbody id="tbody2">
<tr>
<td>
<input type="text" id="bacthNo" class="form-control md-has-value" required="">
</td>
<td>
<input type="text" id="location" class="form-control md-has-value" required="">
</td>
<td>
<input type="text" id="qty" class="form-control md-has-value" required="">
</td>
</tr>
<tr>
<td>
<input type="text" id="bacthNo" class="form-control md-has-value" required="" value="0">
</td>
<td>
<input type="text" id="location" class="form-control md-has-value" required="" value="0">
</td>
<td>
<input type="text" id="qty" class="form-control md-has-value" required="" value="0">
</td>
</tr>
</tbody>
</table>

Enabling form fields in case of minor

form field screenshot
I am collecting nominees data from users for nomination in my organization. The form fields have been attached as image for your reference.
I use iondatepicker for the date field. In case the date of birth chosen by the users for the nominee works out to be the age of a minor i want the fields of the minors' guardian data to be enabled. else they should remain as disabled or with the value 'NA'. Please find below the codes for the form fields.
<table>
<tr>
<td>
<div id="result-1">
<input style="width: 600px;" type="text" value="" name="dob" id="mydate"
data-lang="en" data-years="1925-2018" size="10" maxlength="10"
placeholder='date of birth of nominee (dd/mm/yyyy)' data-format="DD/MM/YYYY"
class="form-control"/>
</div>
</td>
</tr>
<tr>
<td style="text-align: left;">
<b>If Nominee is a Minor, give
GUARDIAN's Particulars</b>
</td>
</tr>
<tr>
<td>
<input style="width: 600px;"
type="text" name="g_name" id="g_name" size="30" maxlength="200"
placeholder='Guardian Name' class="form-control"/>
</td>
</tr>
<tr>
<td>
<input style="width: 600px;"
type="text" name="g_relation" id="g_relation" size="30" maxlength="200"
placeholder='Guardian Relationship with you' class="form-control"/>
</td>
</tr>
<tr>
<td>
<input style="width: 600px;"
type="text" name="g_address" id="g_address" size="30" maxlength="200"
placeholder='Guardian Address' class="form-control"/>
</td>
</tr>
</table>
You can try following solution (assuming you are not using any libraries like jQuery or moment.js, it's in plain javascript):
var myDate = document.getElementById('mydate')
myDate.addEventListener('change', function(e){
var val = e.target.value
var date = new Date(val)
if ( isNaN( date.getDate()) ){
alert('Wrong date format!')
return;
}
var compareDate = new Date()
compareDate.setFullYear(compareDate.getFullYear()-18)
if(compareDate < date){
document.getElementById('g_name').value=""
document.getElementById('g_relation').value=""
document.getElementById('g_address').value=""
document.getElementById('g_name').disabled=false
document.getElementById('g_relation').disabled=false
document.getElementById('g_address').disabled=false
}else{
document.getElementById('g_name').value="NA"
document.getElementById('g_relation').value="NA"
document.getElementById('g_address').value="NA"
document.getElementById('g_name').disabled=true
document.getElementById('g_relation').disabled=true
document.getElementById('g_address').disabled=true
}
})
<table>
<tr>
<td>
<div id="result-1">
<input style="width: 600px;" type="text" value="" name="dob" id="mydate"
data-lang="en" data-years="1925-2018" size="10" maxlength="10"
placeholder='date of birth of nominee (dd/mm/yyyy)' data-format="DD/MM/YYYY"
class="form-control"/>
</div>
</td>
</tr>
<tr>
<td style="text-align: left;">
<b>If Nominee is a Minor, give
GUARDIAN's Particulars</b>
</td>
</tr>
<tr>
<td>
<input style="width: 600px;"
type="text" name="g_name" id="g_name" size="30" maxlength="200" disabled
placeholder='Guardian Name' class="form-control"/>
</td>
</tr>
<tr>
<td>
<input style="width: 600px;"
type="text" name="g_relation" id="g_relation" size="30" maxlength="200" disabled
placeholder='Guardian Relationship with you' class="form-control"/>
</td>
</tr>
<tr>
<td>
<input style="width: 600px;"
type="text" name="g_address" id="g_address" size="30" maxlength="200" disabled
placeholder='Guardian Address' class="form-control"/>
</td>
</tr>
</table>

How to save the content of each column of a row in a jQuery array

I am trying to create an array from the content of a table. The content of the tables looks something like this:
<table>
<tr class="rowUpdate">
<td>Corredor Feed</td>
<td>Id Corrdor
<input type="text" value="" class="validate" name="idcorreo">
</td>
<td>Nombre
<input type="text" value="" class="validate" name="nombre">
</td>
<td>Email
<input type="text" value="foo#bar.com" class="validate" name="email">
</td>
<td>Empressa
<input type="text" value="" class="validate" name="Empressa">
</td>
<td>Pagina Web
<input type="text" value="" class="validate" name="paginaWeb">
</td>
<td>Telefono
<input type="text" value="" class="validate" name="telephon">
</td>
<td>Cellular
<input type="text" value="" class="validate" name="cellular" />
</td>
<td>
<input type="submit" id="guardarBtn" value="Save" name="submitme">
</td>
</tr>
</table>
And the script that I have written is like this
$(document).on('click', '#guardarBtn', function (event) {
var content=[];
$('.rowUpdate').each(function (i) {
$(this).find('td').each(function (j, v) {
if (j != 0) {
var input = $("input", this),
name = input.attr("name").substring(0, input.attr("name").length),
value = input.val();
alert(value);
content[name] = value;
alert(JSON.stringify(content));
}
});
//alert(content);
rows.push(content);
});
});
But when I click on the button to get the content of the columns of the table and save it in an array it shows blank
UPDATED Js Fiddle link is here
Thanks in advance
Check this one. I changed var content= []; to var content= {};
$(document).on('click', '#guardarBtn', function (event) {
var content= {};
$('.rowUpdate').each(function (i) {
$(this).find('td').each(function (j, v) {
if (j != 0) {
var input = $("input", this),
name = input.attr("name").substring(0, input.attr("name").length),
value = input.val();
content[name] = value;
// alert(JSON.stringify(content));
}
});
alert(JSON.stringify(content));
// rows.push(content);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr class="rowUpdate">
<td>Corredor Feed</td>
<td>Id Corrdor
<input type="text" value="" class="validate" name="idcorreo">
</td>
<td>Nombre
<input type="text" value="" class="validate" name="nombre">
</td>
<td>Email
<input type="text" value="foo#bar.com" class="validate" name="email">
</td>
<td>Empressa
<input type="text" value="" class="validate" name="Empressa">
</td>
<td>Pagina Web
<input type="text" value="" class="validate" name="paginaWeb">
</td>
<td>Telefono
<input type="text" value="" class="validate" name="telephon">
</td>
<td>Cellular
<input type="text" value="" class="validate" name="cellular" />
</td>
<td>
<input type="submit" id="guardarBtn" value="Save" name="submitme">
</td>
</tr>
</table>
Try this (keep the HTML the same):
$(document).on('click', '#guardarBtn', function (event) {
var rows = [];
$('.rowUpdate').each(function (i) {
var row = {};
$(this).find('td').each(function (j, v) {
if (j != 0) {
var input = $("input", this),
name = input.attr("name").substring(0, input.attr("name").length),
value = input.val();
row[name] = value;
}
rows.push(row);
});
});
Shorter way to access :
$(document).on('click', '#guardarBtn', function (event) {
var content = {};
$('.rowUpdate').each(function (i) {
$(this).find('input[type=text]').each(function(){
content[$(this).attr("name")] = $(this).val();
});
});
alert(JSON.stringify(content));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr class="rowUpdate">
<td>Corredor Feed</td>
<td>Id Corrdor
<input type="text" value="" class="validate" name="idcorreo">
</td>
<td>Nombre
<input type="text" value="" class="validate" name="nombre">
</td>
<td>Email
<input type="text" value="foo#bar.com" class="validate" name="email">
</td>
<td>Empressa
<input type="text" value="" class="validate" name="Empressa">
</td>
<td>Pagina Web
<input type="text" value="" class="validate" name="paginaWeb">
</td>
<td>Telefono
<input type="text" value="" class="validate" name="telephon">
</td>
<td>Cellular
<input type="text" value="" class="validate" name="cellular" />
</td>
<td>
<input type="submit" id="guardarBtn" value="Save" name="submitme">
</td>
</tr>
</table>
Try This :
<form name="form_name" id='form_name'>
<table>
<tr class="rowUpdate">
<td>Corredor Feed</td>
<td>Id Corrdor
<input type="text" value="" class="validate" name="idcorreo">
</td>
<td>Nombre
<input type="text" value="" class="validate" name="nombre">
</td>
<td>Email
<input type="text" value="foo#bar.com" class="validate" name="email">
</td>
<td>Empressa
<input type="text" value="" class="validate" name="Empressa">
</td>
<td>Pagina Web
<input type="text" value="" class="validate" name="paginaWeb">
</td>
<td>Telefono
<input type="text" value="" class="validate" name="telephon">
</td>
<td>Cellular
<input type="text" value="" class="validate" name="cellular" />
</td>
<td>
<input type="submit" id="guardarBtn" value="Save" name="submitme">
</td>
</tr>
</table>
</form>
js Here :
$(document).on('click', '#guardarBtn', function (event) {
var row=[];
$('.rowUpdate').each(function (i) {
var content=[];
$(this).find('input').each(function(key,value){
var field_name=$(this).attr('name');
var field_value=$(this).val();
content[field_name]=field_value;
});
row.push(content);
});
console.log(row);
});
If I well understand the question, you just need for something like this (I'm not sure however why you used nested each loops - is there any reason of this? Do you have more code in between these loops?)
$(document).on('click', '#guardarBtn', function(e){
var content={};
$('.rowUpdate td').find('input').each(function(){
content[this.name] = this.value;
});
alert(JSON.stringify(content));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr class="rowUpdate">
<td>Corredor Feed</td>
<td>Id Corrdor
<input type="text" value="" class="validate" name="idcorreo">
</td>
<td>Nombre
<input type="text" value="" class="validate" name="nombre">
</td>
<td>Email
<input type="text" value="foo#bar.com" class="validate" name="email">
</td>
<td>Empressa
<input type="text" value="" class="validate" name="Empressa">
</td>
<td>Pagina Web
<input type="text" value="" class="validate" name="paginaWeb">
</td>
<td>Telefono
<input type="text" value="" class="validate" name="telephon">
</td>
<td>Cellular
<input type="text" value="" class="validate" name="cellular" />
</td>
<td>
<input type="submit" id="guardarBtn" value="Save" name="submitme">
</td>
</tr>
</table>

javascript form handling multiple fields

I have a form, echo'd from a php page, and I need to error handle multiple text fields
the 'phone' field is not meant to have more than 10 NUMBERS
the 'fname', 'oname' and 'sname' are meant to have only letters
and the 'date' is meant to have only date format.
How do i achieve this using javascript?
echo '
<html>
<head>
<title>Member Registration</title>
<link rel="stylesheet" type="text/css" href="css.css">
</head>
<!-- USE JAVASCRIPT FOR ERROR HANDLING -->
<body>
<h1>Member Registration</h1>
<form method="POST" enctype="multipart/form-data" action="register.php" name="regform" onsubmit="return validateForm();return allnumeric()">
<table>
<tr>
<td><label for="fname">First name: </label> <input class="fields" type="text" name="fname" value="" required></td>
<td><label for="sname">Surname: </label> <input class="fields" type="text" name="sname" value="" required></td>
<td><label for="oname">Other names: </label> <input class="fields" type="text" name="oname" value="" required></td>
</tr>
<tr>
<td>Choose Profile Picture:</td><td> <input type="file" name="image" id="file" class="fields"></td>
</tr>
<tr>
<td><label for="nationalid" >National ID: </label> <input class="fields" type="text" name="nationalid" value="" required></td>
<td><label for="dob">Date: </label><input class="fields" type="text" name="dob" required></td>
</tr>
<tr>
<td><label for="email">Email: </label> <input class="fields" type="text" name="email" value="" required></td>
</tr>
<tr>
<td><label for="phone">Phone Number: </label> <input class="fields" type="text" name="phone" value="" required></td>
</tr>
<tr>
<td><label for="gender">Male</label><input type="radio" value="M" name="gender" required></td>
<td><label for="gender">Female</label><input type="radio" value="F" name="gender" required></td>
</tr>
<tr>
<td><input type="submit" value="Submit" name="submit" onclick="return matchcheck"></td>
</tr>
</table>
</form>
</body>
</html>';
here is the script i have written so far
function validateForm()
{
var x=document.forms["regform"]["email"].value;
var atpos=x.indexOf("#");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid e-mail address");
return false;
}
}
function isNumeric(){
var a = document.getElementById('phone').value;
var b = /^\d+$/;
if(a.search(b) == -1)
{
alert(Must be Interger);
return false;
}
}
</script>

Categories