Showing posts with label javaScript. Show all posts

regexp: Java script Regular Expression bundle

What is Regular Expression?
Regular expression is machine language. It filter given input based on regular expression. For more information on regular expression read
http://en.wikipedia.org/wiki/Regular_expression

regexp.js

Introducing the javascript bundle that contains bunch of useful Regular expression. I implemented some general regular expression in the javascript however there are tons of other reg exp still pending.

Current version regexp.0.0.1.js

  • Regular Expression Email 
  • Regular Expression hashtag
  • Regular Expression bold 
  • Regular Expression italic
  • Regular Expression strike
  • Regular Expression phone_3_3_4
  • Regular Expression phone_3_7
  • Regular Expression phone_10
  • Regular Expression phone_5_6
  • Regular Expression ipAddress
  • Regular Expression YYYY/MM/DD
  • Regular Expression MM/DD/YYYY
  • Regular Expression DD/MM/YYYY
  • Regular Expression Digit
  • Regular Expression Float
  • Regular Expression username
  • Regular Expression password
  • Regular Expression HEXcolor
  • Regular Expression imageFile
  • Regular Expression documentFile
  • Regular Expression HTMLtag
  • Regular Expression 12Hour
  • Regular Expression 24Hour
  • Regular Expression MAC
  • Regular Expression url
  • Regular Expression badWords



Note:
I request you to all reader to contribute for the javascript. Submit your regular expression:
- Submit your regular expression in comment
- Use Github : https://github.com/javaquery/regexp/blob/master/regexp-0.0.1.js
- Join Google Plus community : https://plus.google.com/communities/116043342143238542756
        - Submit Regular Expression, Request Regular Expression, Report bug, Discussion
- Download : https://github.com/javaquery/regexp/archive/master.zip

Getting started with JSON and Java Web application


What is JSON?
JSON stands for JavaScript Object Notation. Its text-based open standard designed for human-readable data interchange. To know more about JSON read the wikipedia . Here is how JSON object holds the data.

{
    "firstName": "Vicky",
    "lastName": "Thakor",
    "age": 25,
    "address": {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postalCode": 10021
    },
    "phoneNumber": [
        {
            "type": "home",
            "number": "212 555-1234"
        },
        {
            "type": "fax",
            "number": "646 555-4567"
        }
    ]
}

Why use JSON in java web-application?
Its time to develop web-application that perform operation faster. Say if you want to process data without refreshing current page/content JSON is perfect solution for you. I'm going to show you quick demo with validating the email.

Download the JSON library for Java from sourceforge https://sourceforge.net/projects/javaqueryapi/files/


validateEmail.jsp
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Validate Email Address</title>
    </head>
    <body>
        <input name="email" id="email"/>
        <span id="result"></span>
        <input type="button" value="check" id="check"/>
        <script src="../staticCDN/js/jquery-1.8.2.min.js"></script>
        <script>
            $("#check").click(function(){
                var emailID = $("#email").val();
               $.get('emailJSON.jsp',{email:emailID},function(data){
                    var obj = eval ("(" + data + ")");
                    var status = obj.status;
                    if(status=="FAIL"){
                        alert(obj.notify_error);
                    }else{
                        //SUCCESS
                        $("#result").html("Email is Okay!");
                    }
               })
            });
        </script>
    </body>
</html>
emailJSON.jsp
<%@page import="org.json.JSONObject"%>
<%@page import="javaQuery.validation.validate"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
String email = request.getParameter("email");
validate vEmail = new validate();
JSONObject status = new JSONObject();
if(vEmail.Email(email)){
    status.put("status", "SUCCESS");
    status.put("notify_error", "Email is Okay!");
    out.print(status);
}else{
    status.put("status", "FAIL");
    status.put("notify_error", "Provide valid email!");
    out.print(status);
}
%>

Hope you get proper idea of JSON by checking all above codes. If you have any problem let me know.
Output of Demo:
JSON

JSON

Turn urls into links in text with jQuery, Java Script

jQuery

jQuery

Web Technology getting way much faster and easier. jQuery is the Advance version of javascript. jQuery's tag line says it self "Write less, do more". You cant's store everything in database. jQuery helps us to modify the data at the page load time.

I modified some script that helps you to turn your normalized text links to hyperlinks at load time. You can also add required information on that. Lets see how you can achieve this.


Regular Expression

Regular expression is machine language. It filter given input based on regular expression. For more information on regular expression read 
http://en.wikipedia.org/wiki/Regular_expression

Now let me tell you how you can use this javascript. If you want to convert the normalized links into hyperlinks use it as below.

<div id="links">
         http://www.0signals.com
</div>
<script type="text/javascript">
     $(function(){$('#links').doLinks(true);});
</script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="doLinks.js"></script>
//doLinks.js is javascript file you need to create using the below javascript code

/* doLinks.js */
function linkify(hashTagFlag,inputText, options) { 
this.options = { linkClass: 'url', targetBlank: true }; 
this.options = $.extend(this.options, options); 
inputText = inputText.replace(/\u200B/g, ""); 

var replacePattern1 = /(name="[.\\/#a-zA-Z0-9\\(]*|id="[#a-zA-Z0-9\(]*|src="[#a-zA-Z0-9\\(]*|href="[#a-zA-Z0-9\(]*|">|\s>)?(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;ï]*[-A-Z0-9+&@#\/%=~_|ï]/gim; 
var replacedText = inputText.replace(replacePattern1, function($0,$1){ 
    return $1?$0:'<a class="'+ this.options.linkClass + '" href="' + $0 + '"target="_blank">'+ $0+ '</a>'; 
}); 

var replacePattern2 = /(|name="[.\\/#a-zA-Z0-9\\(]*|id="[#a-zA-Z0-9\(]*\(|src="[#a-zA-Z0-9\\(]*\\(|href="[#a-zA-Z0-9\(]*|">|\s>|https?:\/\/|ftp:\/\/)?www\.[-A-Z0-9+&@#\/%?=~_|!:,.;ï]*[-A-Z0-9+&@#\/%=~_|ï]/gim;
var replacedText = replacedText.replace(replacePattern2, function($0,$1){ 
    return $1?$0:'<a class="'+ this.options.linkClass + '" href="http://' + $0 + '"target="_blank">'+ $0+ '</a>';
}); 

var bold = /\*\*(.*?)\*\*/gim; 
var replacedText = replacedText.replace(bold, function($0,$1){ 
    return $1 ? ('<b>' + $1 + '</b>') : $0; 
}); 

var Italic = /\*(.*?)\*/gim; 
var replacedText = replacedText.replace(Italic, function($0,$1){
    return $1 ? ('<i>' + $1 + '</i>') : $0; 
}); 

var Strike = /\--(.*?)\--/gim; 
var replacedText = replacedText.replace(Strike, function($0,$1){ 
    return $1 ? ('<strike>' + $1 + '</strike>') : $0; 
}); 
if(hashTagFlag){
//Works only when you pass true for hashTag
var hashTag = /#(\w*[a-zA-Z_]+\w*)/gim; 
var replacedText = replacedText.replace(hashTag, function($0,$1){ 
    return $1 ? ('<a  href="seach.jsp?query='+$0+'>"' + $0 + '</a>') : $0; 
});
}

var replacePattern3 = /([\.\w]+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim; 
var replacedText = replacedText.replace(replacePattern3, '<a class="' + this.options.linkClass + '" href="mailto:$1">$1</a>'); 
return replacedText; 
}
$.fn.doLinks = function(hashTagFlag){
    this.each(function(){
        $(this).html(linkify(hashTagFlag,$(this).html()));
    });
}

This script works for the below scenario as well. Other scripts can't handle this input.
<a name="9552138821viruddh7(www.songs.pk).mp3" href="viruddh7(www.songs.pk).mp3" onclick=" playmedia(this.name)" id="viruddh7(www.songs.pk).mp3">viruddh7(www.songs.pk).mp3</a>