validate the special character in textbox

Discussion in 'PHP' started by fayee, May 26, 2010.

  1. #1
    Hi,

    I am new to php and javascript. I facing the validated special character in text box.
    in following code is my codes.

    function addRowToTablesaved()
    {
    document.getElementById('btnDeleteselectedsaved').disabled =true;
    document.getElementById('btnDeleteallsaved').disabled =true;
    var tbl = document.getElementById('tblDocsaved');
    var lastRow = tbl.rows.length;
    // if there's no header row in the table, then iteration = lastRow + 1
    var iteration = lastRow;
    document.frmDocsaved.txtActualRowLengthsaved.value = iteration;
    // alert('iteration is: ' + iteration);//jamayah debug
    var row = tbl.insertRow(lastRow);
    if(lastRow%2!=0){
    row.style.backgroundColor = "#85CC2B";
    }else{
    row.style.backgroundColor = "#D9FFBB";
    }
    // 1
    if(iteration<21){
    var cell1 = row.insertCell(0);
    var uploadadd = document.createElement('input');
    uploadadd.type = 'file';
    uploadadd.name = 'userfile' + iteration;
    uploadadd.id = 'userfile' + iteration;
    uploadadd.size = 70;
    cell1.innerHTML = "&nbsp;" + iteration + ".&nbsp;";
    cell1.appendChild(uploadadd);

    //2
    var cell2 = row.insertCell(1);
    var txtCompanyname= document.createElement('input');
    txtCompanyname.type = 'text';
    txtCompanyname.name = 'txtFileDesc' + iteration;
    txtCompanyname.id = 'txtFileDesc' + iteration;
    txtCompanyname.size = 70;

    cell2.innerHTML = "&nbsp;";
    cell2.appendChild(txtCompanyname);

    document.getElementById('btnAddsaved').disabled =false;
    document.getElementById('btnRemovesaved').disabled =false;
    document.getElementById('uploadsaved').disabled =false;
    document.getElementById('btnEditsaved').disabled =true;
    if(document.frmDocsaved.txtActualRowLengthsaved.value == '20'){
    document.getElementById('btnAddsaved').disabled =true;
    }
    }else{
    document.getElementById('btnAddsaved').disabled =true;
    }
    }


    For your information, i try to add onkeyup

    txtCompanyname.onKeyup = function() {
    !(/^[A-zÑñ0-9]*$/i).test(f.value)?f.value = f.value.replace(/[^A-zÑñ0-9]/ig,''):null;
    }


    but it cant work.


    Please write any suggestion to my problems.
    Thanks
     
    fayee, May 26, 2010 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    i just cant understand what you need. can you be more specific?
     
    bartolay13, May 26, 2010 IP
  3. fayee

    fayee Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hi

    i'm doing the validate textbox , so for those special key like !@#$%*()'" when user key in to the test box will automatic backspace.
    i have a button called "add" and it apply this addRowToTablesaved() javascript
    this button's function is addrowtable in my php .

    from my code , user allow to attach files and allow write the file description.
    When the user Key in the special key, the special character will automatic backspace.
    so, i try to .onkeyup to apply this validate function but it not work.

    thanks

    Rgrd
    fayee
     
    fayee, May 26, 2010 IP
  4. kidatum

    kidatum Peon

    Messages:
    61
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I'm not going to code at one in the morning but that is a lot of code for a simple process. Here is a step through in doing what you're trying to achieve:

    1. Write a function so that when the user releases the key, textbox text gets stored in a variable.
    2. You use regex to match special character.
    3. If match returns true remove the last character in the string.
    4. Update textbox text with a new string.
     
    kidatum, May 26, 2010 IP