loops - How to show if date is invalid in javascript -
this tried far still nothing changed. don't start maybe professional can me through this. validation purposes.
code:
$(function(){ var len = $('#groupcontainer > div').length; var arr = []; for(var i=0; < len; i++){ var number = $('#number_' + [i + 1]); var date = $('#date_' + [i + 1]); var count = + 1; var message =""; console.log(number) var = number.map(function(){ return this.value; }); var b = date.map(function(){ return this.value; }); var newobj = {number: a[0], date: b[0]} arr.push(newobj); } var messages={}; var message=""; for(var c = 0; c < arr.length; c++) { var groupmessage=false; for(var d in arr[c]) { if(arr[c].hasownproperty(d)) { if(arr[c][d] == "") { if(messages[d]==undefined) { messages[d]={groups:[]}; } messages[d].groups.push(c+1); } } } } var lastgroup="" for(i in messages){ m = messages[i]; var date = $('#date_' + i); console.log(date) if(m.groups.join(",")==lastgroup) { message = message.replace("group "+m.groups.join(",")+" ","group "+m.groups.join(",")+":\n"); if(m == "date" && date.length != 8) { message += + ' invalid!\n'; } message+=i+" required!\n"; }else { message+="group "+m.groups.join(",")+" "+i+" required!\n"; } lastgroup = m.groups.join(","); } if(message) { alert(message); } });
if field not filled show:
group 1, 2 number required! group 1, 2 date required!
valid date must mm/dd/yy
and if date not date:
group 1, 2 date invalid.
and if 1 of inputs invalid.
group 2 date invalid.
if field filled nothing.
i recommend categorize objects class, example: .txtnumber number .txtdate date
you can use regexpallow keypress event , regexpmatch submit validation.
keypress validation
$(document).on('keyup','.txtdate', function() { var regexpallow = new regexp('[^0-9/]', 'g'); $(this).val($(this).val().replace(regexpallow,'')); }); $(document).on('keyup','.txtnumber', function() { var regexpallow = new regexp('[^0-9.]', 'g'); $(this).val($(this).val().replace(regexpallow,'')); });
submit validation
$(document).on('click','#btnsubmit', function() { var regexpmatch = new regexp('^[0-3][0-9]/[0-1][0-9]/[1-2][0-9]{3}$'); $('.txtdate').each(function() { if ($(this).val().match(regexpmatch) != null) { alert('valid date:'+$(this).val()); } else { alert('invalid date:'+$(this).val()); } }); regexpmatch = new regexp('^[0-9].[0-9]{3}$'); $('.txtnumber').each(function() { if ($(this).val().match(regexpmatch) != null) { alert('valid number:'+$(this).val()); } else { alert('invalid number:'+$(this).val()); } }); });
jsfiddle example: http://jsfiddle.net/bernardobj/o5dbnt51/4/
with can validate characters can entered , how final result (you can validate same way regular expression of numbers if want).
implements logic in code.
note: gets value validate dates , numbers format.
Comments
Post a Comment