Verify Alphabet Textbox only - javascript

How to Verify my text boxes? This is a JSP page created to take in information.
First name should no have Numbers in there, I can enter the name as "103249" and the text box takes it. Also, How do I fix the last name as well?
var btn = "";
function validate(myform) {
if (btn == "submit") {
//Validate Form only on 'submit' button (not for 'clear' button)
var num = 0;
var message = "";
if(myform.firstname.value == "") {
message += "- First Name must be completed \n";
num = 1;
}
if(myform.lastname.value == "") {
message += "- Last Name must be completed \n";
num = 1;
}
if(myform.dateofbirth.value == "") {
message += "- Date of Birth must be completed \n";
num = 1;
}
//RADIO BUTTON VALIDATION
var g = "";
//Loop thru each radio button, verify if "checked"
for (var i=0; i<myform.gender.length; i++) {
if (myform.gender[i].checked) {
//Changes value of "g" if a value is selected
g = myform.gender[i].value;
}
}
//If no radio button selected, "g" will still = ""
if (g == "") {
message += "- Gender must be completed \n";
num = 1;
}
if(myform.gpa.value == "") {
message += "- GPA must be completed \n";
num = 1;
}
//Dropdown
if(myform.education.value == "") {
message += "- Education must be completed \n";
num = 1;
}
//Dropdown
if(myform.job.value == "") {
message += "- Occupation must be completed \n";
num = 1;
}
//RADIO BUTTON VALIDATION
var g = "";
//Loop thru each radio button, verify if "checked"
for (var i=0; i<myform.married.length; i++) {
if (myform.married[i].checked) {
//Changes value of "g" if a value is selected
g = myform.married[i].value;
}
}
//If no radio button selected, "g" will still = ""
if (g == "") {
message += "- Married must be completed \n";
num = 1;
}
//if(myform.spouse.value == "") {
//message += "- Spouse must be completed \n";
//num = 1;
//}
//RADIO BUTTON VALIDATION
var g = "";
//Loop thru each radio button, verify if "checked"
for (var i=0; i<myform.children.length; i++) {
if (myform.children[i].checked) {
//Changes value of "g" if a value is selected
g = myform.children[i].value;
}
}
//If no radio button selected, "g" will still = ""
if (g == "") {
message += "- Children must be completed \n";
num = 1;
}
//Dropdown
//Only require if previous field Children marked "Yes"
if(myform.numchild.value == "0" && g == "Yes") {
message += "- Number of Children must be completed \n";
num = 1;
}
if(myform.childSupport.value == "") {
message += "- Child Support must be completed \n";
num = 1;
}
if(myform.creditScore.value == "") {
message += "- Credit Score must be completed \n";
num = 1;
}
if (num == 1) {
alert ("Please complete or correct the following required fields: \n\n"+message);
return false;
} else {
return true;
} //end if
} //end if submit button
if (btn == "delete") {
//Confirm if want to delete
var result = confirm("Are you sure you want to permanently delete this survey?");
return result;
} //end if delete button
} //end func
//=============== TO DISABLE CCARDS AND NUMBER OF CHILDREN DROPBOXES ==================
function handleSelect(myRadio){
if(myRadio.value == "No"){
document.getElementById("cc_use").selectedIndex = 0; //reset to initial default value
document.getElementById("cc_use").disabled = true;
} else {
document.getElementById("cc_use").disabled = false;
} //end if
} //end func
function handleSelect1(myRadio1){
if(myRadio1.value == "No"){
document.getElementById("numchild").selectedIndex = 0; //reset to initial default value
document.getElementById("numchild").disabled = true;
} else {
document.getElementById("numchild").disabled = false;
} //end if
} //end func
//=============== POPULATE OCCUPATION DROPDOWN BASED ON INDUSTRY DROPDOWN SELECTION ==================
function configureDropDownList(industry, occupation) { //params: id's of sending dropdown & dropdown to change
//Categories: Create a String value of all the list items in the Java ArrayList of Categories
//It translates into a String with opening and closing brackets: [], need to delete brackets from String
var categoryListString = "<%= lstCategories %>";
categoryListString = categoryListString.replace("[","");
categoryListString = categoryListString.replace("]","");
//Split the Category String into a new Javascript array of Categories
var categoryArray = new Array();
categoryArray = categoryListString.split(", "); //Make sure it's comma + space
//Occupations: Create a String value of all the nested list items in the Java ArrayList of ArrayLists: all occup names for each category
//It translates into a String with opening and closing DOUBLE brackets: [[]], need to delete brackets from String
var occupByCatsString = "<%= loccp %>";
occupByCatsString = occupByCatsString.replace("[[","");
occupByCatsString = occupByCatsString.replace("]]","");
//Split the String into a new Javascript array
var occupationsArray = new Array();
occupationsArray = occupByCatsString.split("], [");
//Loop thru array and break into further individual nested arrays for occup names in each category
for (var i = 0; i < occupationsArray.length; i++) {
occupationsArray[i] = occupationsArray[i].split(", "); //Make sure it's comma + space
}
//alert("new occup name array index 0: "+occupationsArray[0]+", index 1: "+occupationsArray[1]);
//alert("Individ occup names in index 0, name 0: "+occupationsArray[0][0]+", name 1: "+occupationsArray[0][1]);
//Categories array (categoryArray) & Nested Occup Names Array by Category (occupationsArray)
//are parallel arrays in order to match Category names with appropriate group of Occup Names
//Loop thru list of all Categories to find selected one
//and create dropdown list of matching Occup Names for that Category
for (var i = 0; i < categoryArray.length; i++)
{
if (industry.value == categoryArray[i]) {
//Call method to create Occupation dropdown list values
//Send array of that Category's list of Occupation Names as parameter, and 'occupation' id value of select
createOccupationItems(occupationsArray[i], occupation);
}
} //end for
} //end function
function createOccupationItems(occupArr, occupation) {
//Parameters in: array of that Category's list of Occupation Names, and 'occupation' id value of select
document.getElementById(occupation).options.length = 0;
for (var i = 0; i < occupArr.length; i++) {
createOption(document.getElementById(occupation), occupArr[i], occupArr[i]);
}
} //end function
function createOption(ddl, text, value) {
var opt = document.createElement('option');
opt.value = value;
opt.text = text;
ddl.options.add(opt);
} //end function
</script>
Here is the actual code to validate:
<div class="sameLine">
<label for="firstname">First Name:</label>
<input type="text" name="firstname" value="<%=surv.getFname()%>">
</div>
<div class="sameLine">
<label for="lastname">Last Name:</label>
<input type="text" name="lastname" value="<%=surv.getLname()%>">
EDIT: Here is the code that I found to verify the letters. It does not work, ignoring it if anything:
if(myform.firstname.value.search(/[^a-zA-Z]+/) === -1) {
message += "- First Name must have letters only \n";
num = 1;
}

Related

Apply multi-prompt validation in Cognos

I have three text prompts and want to validate them as a group, requiring the user to input value in one or more prompts before proceeding.
For e.g. I have First Name, Last Name and Student ID fields and want to group these together such that the user has to input value in at-least one prompt before proceeding. Here is what I have so far, but it is not recognizing the value in second and third fields.
var report = cognos.Report.getReport('_THIS_');
var fName = oCR.prompt.getControlByName('FirstName');
var lName = oCR.prompt.getControlByName('LastName');
var studentId = oCR.prompt.getControlByName('StudentID');
var prompts = [fName ,lName ,studentId];
var i = 0;
var promptsLength = prompts.length;
for ( i = 0; i < promptsLength ; i++) {
prompts[i].setValidator(validate);
}
function validate() {
var result = false;
for ( i = 0; i < promptsLength; i++) {
if (prompts[i].getValues().length > 0) {
result = true;
}
if (prompts[i] != this) {
prompts[i].checkData();
}
}
return result;
}

Checkbox not working for non-repeated array javascript

Can someone one help me make this work:
I have a Checkbox for a non-repeated array. If checkbox is checked to remove duplicates it works fine, but if I uncheck remove duplicates, the checkbox still removes duplicates.
I want to make it work in such a way that if (remove duplicates) checkbox is not checked, extract all emails without removing duplicates.
Thanks.
You were just a couple lines off... when you go to output the emails you need to check for the field that determines if you should use all the emails or the unique (not repeated) emails:
<script type="text/javascript">
function findEmail() {
var email = "No email address detected";
var a = 0;
var ingroup = 0;
var separator = document.extractor.sep.value;
var groupby = Math.round(document.extractor.groupby.value);
var outputEmails = [];
if (separator == "new") separator = "\n";
if (separator == "other") separator = document.extractor.othersep.value;
//match the email pattern in large text pasted in textarea.
var rawemail = document.extractor.rawdata.value.match(/([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
var norepeat = new Array();
if (rawemail) {
for (var i=0; i<rawemail.length; i++) {
var repeat = 0;
// Check for repeated emails routine
for (var j=i+1; j<rawemail.length; j++) {
if (rawemail[i] == rawemail[j]) {
repeat++;
}
}
// Create new array for non-repeated emails
if (repeat == 0) {
norepeat[a] = rawemail[i];
a++;
}
}
// For output, use all or unique (non-repeated) emails?
outputEmails = document.extractor["non-repeated"].checked ? norepeat : rawemail;
if( document.extractor["sortbox"].checked ) {
outputEmails = outputEmails.sort();
}
email = "";
// Join emails together with separator
for (var k = 0; k < outputEmails.length; k++) {
if (ingroup != 0) email += separator;
email += outputEmails[k];
ingroup++;
// Group emails if a number is specified in form. Each group will be separate by new line.
if (groupby) {
if (ingroup == groupby) {
email += '\n\n';
ingroup = 0;
}
}
}
}
// Return array length
var count = norepeat.length;
// Print results
document.extractor.count.value = count;
document.extractor.rawdata.value = email;
}
</script>
JSFiddle example: https://jsfiddle.net/jimbo2150/ucvLtsts/21/

How I can iterate second for loop while using array in JavaScript

I have array which length is 13 .
form has 13 simultaneous fields user can enter in any field i want that if user enter 5 or 7 or any field value then i want add validation that it's previous field value should not be empty and it should not check validation for next field.
I have used this code ...
datesId[0] = "bankNocForTorDateId";
datesId[1] = "advertisingDateShortlistingId";
datesId[2] = "torShortlistFinalizedDateId";
datesId[3] = "bankNocForShortlistDateId";
datesId[4] = "rfpDraftToBankDateId";
datesId[5] = "bankNocForRfpDateId";
datesId[6] = "rfpIssuedDateId";
datesId[7] = "proposalReciptDateTechnicalId";
datesId[8] = "evaluationFinalTechnicalDateId";
datesId[9] = "bankNocTechnicalDateId";
datesId[10] = "proposalReciptDateFinancialId";
datesId[11] = "evaluationFinalCombinedDateId";
datesId[12] = "nocBankDraftDate";
for(var i = 0; i<datesId.length ; i++ ){
if(!(document.getElementById (datesId[i]).value == "")){
for(var j =datesId[i].length-1 ;j>0 ; j-- ){
if(document.getElementById(datesId[j]).value == ""){
var message = "Please Enter "+datesLabel[j];
alert(message);
return false;
}
}
}
}
Actually i am new in Javascript...not having much idea about it.I have made this logic on the basis of java. please clearify what's basic difference.
Thanks in Advance
Done!
var datesId = []
datesId[0] = "bankNocForTorDateId";
datesId[1] = "advertisingDateShortlistingId";
datesId[2] = "torShortlistFinalizedDateId";
datesId[3] = "bankNocForShortlistDateId";
datesId[4] = "rfpDraftToBankDateId";
datesId[5] = "bankNocForRfpDateId";
datesId[6] = "rfpIssuedDateId";
datesId[7] = "proposalReciptDateTechnicalId";
datesId[8] = "evaluationFinalTechnicalDateId";
datesId[9] = "bankNocTechnicalDateId";
datesId[10] = "proposalReciptDateFinancialId";
datesId[11] = "evaluationFinalCombinedDateId";
datesId[12] = "nocBankDraftDate";
// if datesId[6] value is not blank
if (datesId[6] != ''){
// validate previous 5 field values should not be blank, but it should not check next 6 value.
for (var i = (6-5); i < 6; i ++){
console.log('validating field ' + datesId[i] + " (index=" + i + ")")
if (datesId[i] == ''){
alert('Field ' + datesId[i] + " must not be blank!")
}
}
}

How to get controls by dynamic name in javascript

I have 5 dropdown with name postfix by 1,2,3...so on... How can i get name of these dropdown in javascript
var TotalLvl=document.getElementById('<%=dd_Designation_Level.ClientID%>').value;
for (var i = 1; i <= d; i++) {
var DesLvl='Designation' +i;
if ((document.getElementById('<%=' + DesLvl +'%>').value == "0")) {
alert('Designation' + i 'should be selected');
}
}
i am not able to do this by this dynamic name DesLvl

Why doesn't it work for rows 7-13

The program is a Seat reservation program rows 1 and 2 are first class, row 3-9 are business class and rows 10 through 13 are economy. They are initialized to * and become X when it is reserved. and alert you if the seat is already filled.
The rows will fill for the first 6 rows but it ignores request for row 7 and beyond. But i know it goes to the appropriate function because if i request Economy with row 3 (which is invalid) it alerts me. But if i ask for first class with row7 it will act like nothing has been requested.
It won't let me post the html code but it will display as row1-13 on the left and A - F across the top. I use drop down list and I set the values to the corresponding for the options to their corresponding spot in a the 2-d array (eg if i select row 5 and seat c the value of row 5 is 5 and the value of C is 3)
var rowSeat;
function start()
{
rowSeat = new Array(14);
for(var i = 0; i <rowSeat.length; i++)
{
rowSeat[i] = new Array(7);
}
var j = 65;
for(var i = 1; i <rowSeat[0].length; i++)
{
rowSeat[0][i] = String.fromCharCode(j);
j++;
}
rowSeat[0][0] = " ";
for(var i = 1; i <rowSeat.length; i++)
{
rowSeat[i][0] = "Row "+i;
}
for(var i = 1; i <rowSeat.length; i++)
{
for(var j = 1; j <rowSeat[i].length; j++)
{
rowSeat[i][j] = "*";
}
}
display();
var subButton = document.getElementById("submitButton");
subButton.addEventListener("click", assign, false);
}
function display()
{
var results = "";
results+="<table>"
for(var i in rowSeat)
{
results+="<tr>";
for(var j in rowSeat[i])
{
results += "<td>" +rowSeat[i][j]+ "</td>";
}
results += "</tr>";
}
results+="</table>"
var show2 = document.getElementById( "show2" );
show2.innerHTML = results;
}
function assign()
{
var inputField = document.getElementById("classAssign");
var classType = inputField.options[inputField.selectedIndex].value;
if (classType == "FirstClass")
{
fClassSearch();
}
else if (classType == "Business")
{
bClassSearch();
}
else
{
eClassSearch();
}
display();
}
function fClassSearch(){
var inputField = document.getElementById("seatAssign");
var seat = inputField.options[inputField.selectedIndex].value;
var inputField2 = document.getElementById("rowAssign");
var row = inputField.options[inputField2.selectedIndex].value;
var test = document.getElementById( "test" );
test.innerHTML = row +" "+ seat;
if (row >2){
var show2 = document.getElementById( "show" );
show.innerHTML = "Invalid choice only row 1 and 2 are First Class";
}
else {
if(rowSeat[row][seat] == "*")
{
rowSeat[row][seat] = "X";
show.innerHTML = "Your Seat choice was accepted and Reserved";
}
else{
show.innerHTML = "Your choice was already reserved please make another choice";
}
}
}
function bClassSearch(){
var inputField = document.getElementById("seatAssign");
var seat = inputField.options[inputField.selectedIndex].value;
var inputField2 = document.getElementById("rowAssign");
var row = inputField.options[inputField2.selectedIndex].value;
if (row <3 ||row >9){
var show2 = document.getElementById( "show" );
show.innerHTML = "Invalid choice only row 3 through 9 are BusinessClass";
}
else {
if(rowSeat[row][seat] == "*")
{
rowSeat[row][seat] = "X";
show.innerHTML = "Your Seat choice was accepted and Reserved";
}
else{
show.innerHTML = "Your choice was already reserved please make another choice";
}
}
}
function eClassSearch(){
var inputField = document.getElementById("seatAssign");
var seat = inputField.options[inputField.selectedIndex].value;
var inputField2 = document.getElementById("rowAssign");
var row = inputField.options[inputField2.selectedIndex].value;
var show1 = document.getElementById( "show" );
if (row <10){
show1.innerHTML = "Invalid choice only rows 10 through 13 are Economy Class";
}
else {
if(rowSeat[row][seat] == "*")
{
rowSeat[row][seat] = "X";
show.innerHTML = "Your Seat choice was accepted and Reserved";
}
else{
show.innerHTML = "Your choice was already reserved please make another choice";
}
}
}
window.addEventListener("load",start, false);
</script>
var row = inputField.options[inputField2.selectedIndex].value;
inputField.options should be inputField2.options
inputField.options only goes to 6 because you only have 6 seats wide, but you are trying to look at the row in the seat list.

Categories