#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Regex problem


    Hello brothers, how are you all??
    i am writing regex of two fields, i want my code to be more reducible. can i write them in one if condition. Or if there is any other way?

    Code:
    function fstnreg() 
    <input type="text" id="fname" size="22" value="First" onKeyPress="fstnreg();" onFocus="fnfocus();" onBlur="fnblur();"/>	
    					<input type="text" id="lname" size="22" value="Last" onKeyPress="lstnreg();"/>
    
    
    { 	var fstname=document.getElementById('fname').value; 	var lstname=document.getElementById('lname').value; 	var frg=/^([a-zA-Z])/; 	if(!fstname.match(frg)||!lstname.match(lrg)) 	{ 		document.getElementById('fnvali').innerHTML='Invalid name'; 		document.getElementById('fnvali').style.Color='red'; 	}  }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Regex problem


    Hello brothers, how are you all??
    i am writing regex of two fields, i want my code to be more reducible. can i write them in one if condition. Or if there is any other way?

    Code:
     
    <input type="text" id="fname" size="22" value="First" onKeyPress="fstnreg();" onFocus="fnfocus();" onBlur="fnblur();"/>	
    <input type="text" id="lname" size="22" value="Last" onKeyPress="lstnreg();"/>
    
    function fstnreg()
    { 	
    var fstname=document.getElementById('fname').value; 	
    var lstname=document.getElementById('lname').value; 	
    var frg=/^([a-zA-Z])/; 	if(!fstname.match(frg)||!lstname.match(lrg)) 	
    { 
    document.getElementById('fnvali').innerHTML='Invalidname'; 		
    document.getElementById('fnvali').style.Color='red'; 
    	}
      }
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,927
    Rep Power
    1045
    Hi,

    um, don't you already have the two checks in one "if" statement?

    But I think the problem is that your functions are far too specific. Writing a function for "blur the element #fname" makes no sense. Define a general function blur() and refer to the current element via "this".
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Regex Problem.


    Thanks buddy.but unfortunately the thing that i know about 'this' keyword after searching a lot is "when we use this keyword in a function and call that function on any event in an <input> tag then 'this' keyword points toward that <input> tag".
    So please tell me how to use it briefly.

    Originally Posted by Jacques1
    Hi,

    um, don't you already have the two checks in one "if" statement?

    But I think the problem is that your functions are far too specific. Writing a function for "blur the element #fname" makes no sense. Define a general function blur() and refer to the current element via "this".

IMN logo majestic logo threadwatch logo seochat tools logo